WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] meta description php code (3 posts)

  1. jrcollins
    Member
    Posted 1 year ago #

    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 } ?>
  2. Rab
    Member
    Posted 1 year ago #

    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)

  3. jrcollins
    Member
    Posted 1 year ago #

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

    `

Topic Closed

This topic has been closed to new replies.

About this Topic