• Resolved jrcollins

    (@jrcollins)


    I’m using the following code in my header template file to generate meta-descriptions. It seems to be working okay for my home page and for posts without a manual excerpt. However, for the posts which have manual excerpts added it just grabs the first few lines of the post.

    <?php if (is_single()) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <meta name="description" content="<?php the_excerpt(); ?>" />
            <?php endwhile; endif; elseif(is_home()) : ?>
            <meta name="description" content="my home page meta-description here" />
            <?php endif; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • the_excerpt() outputs html code which shold not be in a meta description, and can cause severe problems if the excerpt contains double quotes etc.

    instead of:
    <?php the_excerpt(); ?>
    possibly try:
    <?php echo esc_attr(strip_tags(get_the_excerpt())); ?>

    https://php.net/manual/en/function.strip-tags.php
    https://codex.www.ads-software.com/Function_Reference/esc_attr
    https://codex.www.ads-software.com/Function_Reference/get_the_excerpt

    if this is not it, and your actual problem is different:

    for the posts which have manual excerpts added it just grabs the first few lines of the post.

    it might help if you could post a link to each type of post to illustrate the exact problem.

    (untested)

    Thread Starter jrcollins

    (@jrcollins)

    Ok, thanks for that.
    It’s just the posts for which I have added my own excerpt that I’m having a problem with. Instead of the excerpt which I wrote for that particular post it’s putting in an automatically generated meta-description (ie., the first few lines of the post).

    Instead of the excerpt which I wrote for that particular post it’s putting in an automatically generated meta-description (ie., the first few lines of the post).

    theoretically, the code the_excerpt() should output the handwritten excerpt if one exists;
    https://codex.www.ads-software.com/Function_Reference/the_excerpt

    I can only imagine that one of the plugins is interfering;

    temporarily deactivate all plugins to see if that changes anything.
    then reactivate one plugin after the other to locate the problematic one.

    Thread Starter jrcollins

    (@jrcollins)

    Thanks again for the advice. I found these comments when searching through the forum which seem to suggest that even with a custom excerpt google will sometimes still use an automatically generated excerpt for the meta-description.

    google will sometimes still use an automatically generated excerpt for the meta-description.

    misleading – I assumed you were checking the actual output of your theme – not any google search results ??

    Thread Starter jrcollins

    (@jrcollins)

    Okay, sorry about that! I should have made it clearer.

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