WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] meta description php code

[Resolved] meta description php code

  • I just recently added the following code to my header file to generate dynamic meta descriptions for my pages but it doesn’t seem to be working. Can anyone tell me if the code is correctly formatted?

    <?php if ( is_home() ) { ?>
            <meta name="description" content="the meta description for my   home page here" />
            <?php } elseif ( is_single() ) { ?>
            <meta name="description" content="<?php the_excerpt();?>"/>
            <?php } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • is_single returns false for pages, that’s for posts.

    There is detailed information on using Conditional Tags in the Codex. There is good examples there too.

    Perhaps you want to try something like,

    <?php
    if ( is_home() ) { ?>
    	<meta name="description" content="homepage some text"/>
    <?php } else { ?>
    	<meta name="description" content="everything else some text"/>
    <?php }
    ?>

    Which will set the homepage differently to everything else. (untested code)

    Actually I meant posts, not pages. Is the following code correctly formatted?

    <?php if ( is_home() ) { ?>
            <meta name="description" content="the meta description for my   home page here" />
            <?php } elseif ( is_single() ) { ?>
            <meta name="description" content="<?php the_excerpt();?>"/>
            <?php } ?>

    `

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] meta description php code’ is closed to new replies.