0

I want to add a meta data in the head of my website.

I have a post type with custom template : single-custom.php

In this template, I want to add this meta : <meta name="robots" content="noindex"> considering some variable.

I collect number of post related to the current post... I use :

// if less than 6 related posts, add meta
if (count($posts) < 6) {
      add_action( 'wp_head', 'noindex_meta' );
}

And in my functions.php :

// add noindex meta to head
function noindex_meta() {
    echo '<meta name="robots" content="noindex">';
}

But it doesn't work, it works if I call add_action(...) in my functions.php, but not in theme template.

Is it possible ?

  • (Ignore my previous comment. But once again,) in that theme template, if you want to hook noindex_meta() to wp_head, you should do it before get_header() call in the template file. See this. – Sally CJ Oct 26 '18 at 16:35
  • 1
    I put the code before get_header() in my template page, it works perfectly. Thanks for the advice. – Vincent Decaux Oct 26 '18 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.