2014-06-21 139 views
1

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!

回答

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(); ?>" />