Wordpress 页面描述

Wordpress 页面描述,wordpress,meta-tags,Wordpress,Meta Tags,在上给出的答案的帮助下,我想我已经基本解决了这个问题,但它似乎不起作用-当我查看任何页面的页面源代码时,元描述是空的: <meta name="description" content="" /> 以下是我得到的: 在functions.php中 <?php if( is_single() || is_page() ) $description = get_the_excerpt(); elseif( is_category() ) $description = cate

在上给出的答案的帮助下,我想我已经基本解决了这个问题,但它似乎不起作用-当我查看任何页面的页面源代码时,元描述是空的:

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

以下是我得到的:

在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);
?>

在标题中

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

试试这个函数,在大多数情况下它会返回一些东西

// 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(); ?>" />
//functions.php
函数blog_description(){
$content=get_queryed_object();
如果(是单数()){
$content=!empty($content->post\u extract)?$content->post\u extract:(!empty($content->post\u content)?$content->post\u content:$content->post\u title);
返回str_replace(PHP_EOL',substr(wp_filter_nohtml_kses($content),0155));
}elseif(is_category()){
$content=!empty($content->description)?$content->description:get_bloginfo('name')。-。$content->name;
返回str_replace(PHP_EOL',substr(wp_filter_nohtml_kses($content),0155));
}
返回get_bloginfo('description');
}
//header.php