david
david

Reputation: 1127

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.

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

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

Upvotes: 9

Views: 45625

Answers (11)

Andras
Andras

Reputation: 11

You could also do something like this. Put it in your functions.php or with a plugin like Code Snippets.

add_action( 'wp_head', 'my_header_code' );

function my_header_code() {
    if ( is_page( 'ID' ) ) {
        // Do your stuff
    }
}

Upvotes: 1

kuldip Makadiya
kuldip Makadiya

Reputation: 715

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.

Upvotes: 12

upss1988
upss1988

Reputation: 173

Try with this:

if (is_page('page_slug') { ?>
   <META name="robots" content="noindex, nofollow"/>
<?php }

You can check the reference here: https://developer.wordpress.org/reference/functions/is_page/

Upvotes: 0

marklchaves
marklchaves

Reputation: 44

This worked for me. I didn't feel I needed to get the post type. I just needed the ID.

<?php
$post = get_post();
if ( $post->ID == 1234 ){
    echo '<!-- Point to the AMP version of this page. -->';
    echo '<link rel="amphtml" href="/amp/amp-story">';
}
?>

This did not work for me as spec'd in the Codex (https://developer.wordpress.org/reference/functions/is_page/).

is_page( 1234 )

Upvotes: 0

ingvar3000
ingvar3000

Reputation: 21

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.

Upvotes: 0

dewaz
dewaz

Reputation: 147

 <?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.

Upvotes: -1

Igor
Igor

Reputation: 1737

There's a plugin to do exactly the thing requested.
HiFi (Head Injection, Foot Injection)

There's a "This plugin hasn't been updated in over 2 years...." notification, but I've just tested it on WP 3.5.2 - it works.

Upvotes: 0

Chetan
Chetan

Reputation: 102

<?php global $post; ?>
<?php if(is_page('pagename')){ ?>
      <meta name="robots" content="noindex, nofollow" />
<?php } ?>

Upvotes: 0

ArleyM
ArleyM

Reputation: 853

Custom Fields are a great way to allow you to have page specific meta data that you can create logic on in your template files. The Codex link gives great examples of this.

Upvotes: 0

nouveau
nouveau

Reputation: 1262

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

Upvotes: 11

user1409909
user1409909

Reputation:

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" />';
}

Upvotes: 2

Related Questions