Meta description

With the help of the answer given at Place category_description in the meta description (wordpress) I think I’ve got this almost figured out, but it just doesn’t seem to be working – when I view the page source for any page, the meta description is empty:

<meta name="description" content="" />

Here’s what I’ve got:

Read More

In functions.php

<?php
if( is_single() || is_page() ) $description = get_the_excerpt();
elseif( is_category() ) $description = category_description();
else $description = "Free French lessons and language tools from Laura K. Lawless, including verb conjugations and bilingual articles to help you improve your reading and listening comprehension.";
$description = substr($description,0,500);
?>

In header

<meta name="description" content="<?= $description ?>" />

Any ideas? TIA!

Related posts

Leave a Reply

1 comment

  1. Try this function, it will return something in most cases.

    // functions.php
    function blog_description() {
        $content = get_queried_object();
    
        if ( is_singular() ) {  
            $content = !empty( $content->post_excerpt ) ? $content->post_excerpt : ( !empty( $content->post_content ) ? $content->post_content : $content->post_title );
            return str_replace( PHP_EOL, ' ', substr( wp_filter_nohtml_kses( $content ), 0, 155 ) );
    
        } elseif ( is_category() ) {        
            $content = !empty( $content->description ) ? $content->description : get_bloginfo( 'name' ) . ' - ' . $content->name;       
            return str_replace( PHP_EOL, ' ', substr( wp_filter_nohtml_kses( $content ), 0, 155 ) );    
        }
    
        return get_bloginfo( 'description' );
    }
    
    // header.php
    <meta name="description" content="<?php echo blog_description(); ?>" />