在Place category_description in the meta description (wordpress)给出的答案的帮助下,我认为我已经得到了这个几乎已经弄清楚,但它似乎无法正常工作 - 当我查看任何页面来源时页面,元描述为空:
<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 ?>" />
有什么想法吗? TIA!
答案 0 :(得分:2)
尝试此功能,在大多数情况下会返回一些内容。
// 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(); ?>" />