WordPress- How to insert code to header for a specific page only?

I use wordpress and would like to add 2 lines of code to the header of one page only.

The problem is that header.php will change all the site’s headers and I want it to change only the header of one specific page.

Read More

The only thing I want to do is add this 1 line :

<META name="robots" content="noindex, nofollow"/>

Related posts

Leave a Reply

10 comments

  1. If you want to have a different header for a certain page you need to download your header.php from FTP, rename it to header-new.php (replace “new” with whatever you want), re-upload header-new.php to the same directory as your original header.
    – On the page template you want the new header to show up on

    replace:

    <?php get_header(); ?> 
    

    with

    <?php get_header('new'); ?>
    

    and now your new header will show up only on that specific page template

  2. you have to just add your pageid on your header file like this

    global $post;
    if($post->post_type == 'page' && $post->ID == page_int){
       echo '<meta name="robots" content="noindex, nofollow" />';
    }
    

    it will just display meta on specific page which you want.

    Suppose you only want to output the code for the page with ID = 5, set page_int to 5. This is an integer, so do not use single quotes around it.

  3. Just correcting the answer of FDL, use this:

    global $post;
    if($post->post_type == 'page' && $post->ID == 'yourid'){
       echo '<meta name="robots" content="noindex, nofollow" />';
    }
    
  4. An easier, non-coding solution to changing the robots meta tag on a per-page basis is by using the Yoast SEO plugin.
    https://yoast.com/wordpress/plugins/seo/
    You can set individual pages (such as a form thank-you page) as noindex and even nofollow if you’re so inclined. If you’re using Yoast to generate your sitemap as well, then you can exclude that page from the sitemap at the same time you noindex it, which will prevent errors in Search Console.

    If you want to have the ability to add some other tags or esoteric syntax to the then you can use the Per Page Add to Head plugin
    https://wordpress.org/plugins/per-page-add-to/
    Which will allow you to be very granular about which page gets which code.

  5.  <?php if (is_home()) { ?>
        <META name="robots" content="noindex, nofollow"/>
     <?php } ?>
    

    this code will put meta only for home page, you may use is_single, is_archive, is_404 etc.