WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic meta description for category, home, and pages using conditional tags (7 posts)

  1. joelwalsh
    Member
    Posted 9 years ago #

    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.
    <?php
    if (is_single()) {
    get_posts('numberposts=1');
    foreach ($posts as $post) :
    $post_excerpt = $post->post_excerpt;
    endforeach;}
    ?>

  2. Kafkaesqui

    Posted 9 years ago #

    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:
    http://wordpress.org/support/topic/42126#post-236872

  3. joelwalsh
    Member
    Posted 9 years ago #

    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:

    http://upmarketnewsletters.com/articles/sticky-post-2/

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />

    <title><?php
    if (is_home()) {
    bloginfo('name');}
    else {wp_title();}
    ?>
    </title>

    <?php
    if (is_single()) {
    get_posts('numberposts=1');
    foreach ($posts as $post) :
    $post_excerpt = $post->post_excerpt;
    endforeach;}
    ?>
    <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.

  4. joelwalsh
    Member
    Posted 9 years ago #

    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?

  5. Kafkaesqui

    Posted 9 years ago #

    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;

  6. jtwright
    Member
    Posted 8 years ago #

    This worked perfectly for me. Download the following plugin:

    http://guff.szub.net/downloads/head-meta-desc.zip

    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.

  7. freotech
    Member
    Posted 8 years ago #

    hi,
    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
    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic