Ad

How To Exclude Empty Page Only From Being Indexed In Google On WordPress

- 1 answer

I've created more than 10k pages on WordPress and to avoid having a Panda penalty on Google for having thin content, what i did is i put JavaScript code on loop-page.php and check the page if there is no content and added meta tags for disallowing the page to be crawled but it doesn't work all the time. Here is the code I've added.

<?php if($post->post_content=="") { ?>
<p>We are still updating our website with contents. Please check back next time.</p>
<script>
    (function(){
        var meta = document.createElement("meta");
        meta.setAttribute("name",  "robots");
        meta.setAttribute("content",  "noindex, nofollow");
        document.head.appendChild(meta);
        
        var meta2 = document.createElement("meta");
        meta2.setAttribute("name",  "googlebot");
        meta2.setAttribute("content",  "noindex");
        document.head.appendChild(meta2);
    })()
</script>

Can you please give me an idea to do this properly? It looks like my JavaScript won't run when Google crawls the page and btw I'm using SEO Ultimate plugin

Ad

Answer

Meta tags need to be hard-coded in order to function. If you want to dynammically create meta tags you need to do it server-side. With PHP:

if(!pageHasContent){
  echo "<meta name='robots' content='noindex, nofollow' />";
  echo "<meta name='googlebot' content='noindex' />";
}
Ad
source: stackoverflow.com
Ad