1

I need help with this

/* meta description to head */
add_action( 'wp_head','carlos_head_meta_page' );  
function carlos_head_meta_page() { ?> 
    <meta name="description" content="vale here" />
<?php }

It works fine, but I would like to use a conditional tag like

if ( is_page('4') )

So I can use different meta tags for specific pages.

closed as off-topic by Michael, Rarst, bueltge, Jack Johansson, kraftner Nov 8 '17 at 8:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    So use the tag then, what’s the problem? – Jacob Peattie Oct 19 '17 at 1:12
  • I need to know the correct syntax as when I use it it won't work. – Losrack Oct 19 '17 at 2:00
  • Can you at least post the code that doesn't work. – Jacob Peattie Oct 19 '17 at 2:04
  • if ( is_page('4') ) add_action( 'wp_head','carlos_head_meta_page' ); function carlos_head_meta_page() { ?> <meta name="description" content="vale here" /> <?php } – Losrack Oct 19 '17 at 2:47
  • you need to put the conditional inside the function as the page query is not populated until later so is_page will not work outside of it. ie. add_action( 'wp_head','carlos_head_meta_page' ); function carlos_head_meta_page() { if (is_page('4')) { ?> <meta name="description" content="vale here" /> <?php } } – majick Oct 19 '17 at 4:34
3

If you want to use different meta tags for different pages I recommend you to use custom field values.

If you prefer to do it with conditional functions, try this:

add_action( 'wp_head','carlos_head_meta_page' );  
function carlos_head_meta_page() { 

    if ( is_page('4') ) {
        $description = 'tag1';
    } else {
        $description = 'tag2';
    }
    ?> 
    <meta name="description" content="<?php echo $description ?>" />
    <?php

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.