2014-06-21 170 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(); ?>" />