Support » Fixing WordPress » Dynamic meta description for category, home, and pages using conditional tags

  • I’m using the following line of code to create meta descriptions for posts using post_excerpt. How do I modify this to show the tagline in the homepage meta description, the category description in the category description, and a custom field in the page description? The get_posts part has me confused.
    if (is_single()) {
    foreach ($posts as $post) :
    $post_excerpt = $post->post_excerpt;

Viewing 6 replies - 1 through 6 (of 6 total)
  • First, do you realize your code does nothing? For foreach ($posts as $post) to work, $posts must be assigned the output of get_posts():

    $posts = get_posts('numberposts=1');

    Second, why are you using get_posts? It’s purpose is to collect one or more posts (in this case, just 1, that being the last one posted on the blog) and make use of them in whatever way you need. But it cannot call up a specific post, which is what you need to be able to do do to collect the *current* post.

    Last but not least, see my plugin:

    It doesn’t work? I’m certainly not knowledgeable in php (I copied that bit of code from something else I can’t remember), but once I got the thing to display the excerpt in the meta description in the head of the html document, I stopped tinkering with it . As you can see on this page, it does work for outputting a meta description in a page that uses it:

    This is from my header.php (it ends with the meta description):

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
    <html xmlns=””>
    <meta http-equiv=”Content-Type” content=”<?php bloginfo(‘html_type’); ?>; charset=<?php bloginfo(‘charset’); ?>” />

    if (is_home()) {
    else {wp_title();}

    if (is_single()) {
    foreach ($posts as $post) :
    $post_excerpt = $post->post_excerpt;
    <meta name=”description” content=”<?php echo htmlentities($post_excerpt); ?>” />

    Perhaps you didn’t realize I was using the last block of code to display the previously called entities? I hadn’t bothered to paste it in because it was not relevant to my query and I thought it would be obvious I was using something like that because without it I could never get the excerpt to display.

    Great plugin! I just installed it. I had been searching for something like this very hard and was surprised that there wasn’t a plugin for this. Any chance of this being included in the core next time?

    I doubt it, but that would be up to the developers. You can always suggest…

    Note that you’re correct about get_posts running as you have it; it does *work* without assigning a var. Stupid assumptions…

    However (if for nothing else but to get back some coding cred!), get_posts() is really not returning its own $posts object here. This gets clobbered by the one normally available to a post, and which is why your code displays the current post’s excerpt (which it “shouldn’t” be doing). So using get_posts() this way is overkill and generates an useless database query. To anyone doing this I’d recommend replacing it with the following, as it simply makes the existing $post object accessable to ones code:

    $post = $wp_query->post;
    $post_excerpt = $post->post_excerpt;

    This worked perfectly for me. Download the following plugin:

    Extract the head-meta-desc.php, upload it to your wp-content/plugins directory and then activate ‘Head Meta Description’ under Plugins in the Dashboard.

    Insert the following code into your <head></head> section (generally found in the header.php file):

    <?php head_meta_desc(); ?>

    This will generate the following code:

    <meta name=”description” content=”(whatever the first X number or words you have for that page’s post)” />

    There are a couple variables in the plugin: $desc_length sets the number of words of the post or Page to place in the meta tag. Very useful if you want to add more or less info for the description’s content. I personally have mine set to 50 words.

    I read about tweakin head meta desc so that you can put a custom description for your main page.
    The tweak says that if the string-length of the description tag = 0, then it reverts to a separate description tag. Thus on your index.php page or any other page where there’s no specific excerpt to use, it’ll revert to your default description. The tweak is as follows:

    if ( strlen( trim( $description )) == 0 ) {

    $description = ‘Hotdogs rock my world… Yum’;



    I tried to modify the head meta desc plugin, but am getting a parsing error .

    Can someone correct the error

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Dynamic meta description for category, home, and pages using conditional tags’ is closed to new replies.