???????Wordpress網站的關鍵字及網頁描述關系網站對搜索引擎的友好程度,如果自己手動加顯然太折騰了,那如何讓WordPress博客自動為每篇文章自動關鍵字及網頁描述。每篇文章的內容不同,我們該如何讓wordpress自動添加文章描述和關鍵詞呢?下面就讓我們來看看如何給wordpress自動添加文章描述和關鍵詞。
重構:首頁,分類頁,文章頁,單頁,搜索頁面,標簽頁,專題,快訊頁,作者頁,404等
第一種:
在你主題的functions.PHP文件添加以下代碼,各個代碼的功能解析如下:
add_action ( 'wp_head', 'wp_keywords' ,1 ); // 添加關鍵字
add_action ( 'wp_head', 'wp_description' ,1 ); // 添加頁面描述/**+----------------------------------------------------------* 站點關鍵字+----------------------------------------------------------* @return string+----------------------------------------------------------*/
function wp_keywords() {global $s, $post;$keywords = '';if (is_single ()) { //如果是文章頁,關鍵詞則是:標簽+分類IDif (get_the_tags ( $post->ID )) {foreach ( get_the_tags ( $post->ID ) as $tag )$keywords .= $tag->name . ', ';}foreach ( get_the_category ( $post->ID ) as $category )$keywords .= $category->cat_name . ', ';$keywords = substr_replace ( $keywords, '', - 2 );} elseif (is_home ()) {$keywords = '我是主頁關鍵詞'; //主頁關鍵詞設置} elseif (is_tag ()) { //標簽頁關鍵詞設置$keywords = single_tag_title ( '', false );} elseif (is_category ()) {//分類頁關鍵詞設置$keywords = single_cat_title ( '', false );} elseif (is_search ()) {//搜索頁關鍵詞設置$keywords = esc_HTML ( $s, 1 );} else {//默認頁關鍵詞設置$keywords = trim ( wp_title ( '', false ) );}if ($keywords) { //輸出關鍵詞echo "<meta name=\"keywords\" content=\"$keywords\" />\n";}
}/**+----------------------------------------------------------* 站點描述+----------------------------------------------------------* @return string+----------------------------------------------------------*/
function wp_description() {global $s, $post;$description = '';$blog_name = get_bloginfo ( 'name' );if (is_singular ()) { //文章頁如果存在描述字段,則顯示描述,否則截取文章內容if (! empty ( $post->post_excerpt )) {$text = $post->post_excerpt;} else {$text = $post->post_content;}$description = trim ( str_replace ( array ("\r\n","\r","\n"," "," " ), " ", str_replace ( "\"", "'", strip_tags ( $text ) ) ) );if (! ($description))$description = $blog_name . "-" . trim ( wp_title ( '', false ) );} elseif (is_home ()) {//首頁顯示描述設置$description = $blog_name . "-" . get_bloginfo ( 'description' ) .'首頁要顯示的描述'; // 首頁要自己加} elseif (is_tag ()) {//標簽頁顯示描述設置$description = $blog_name . "有關 '" . single_tag_title ( '', false ) . "' 的文章";} elseif (is_category ()) {//分類頁顯示描述設置$description = $blog_name . "有關 '" . single_cat_title ( '', false ) . "' 的文章";} elseif (is_archive ()) {//文檔頁顯示描述設置$description = $blog_name . "在: '" . trim ( wp_title ( '', false ) ) . "' 的文章";} elseif (is_search ()) {//搜索頁顯示描述設置$description = $blog_name . ": '" . esc_html ( $s, 1 ) . "' 的搜索結果";} else {//默認其他頁顯示描述設置$description = $blog_name . "有關 '" . trim ( wp_title ( '', false ) ) . "' 的文章";}//輸出描述$description = mb_substr ( $description, 0, 220, 'utf-8' ) . '..';echo "<meta name=\"description\" content=\"$description\" />\n";
}
第二種:
//Title標題
function bzg_filter_title( $title ) {$title['site'] = '';$title['tagline'] = '';$title['page'] = '';return $title;
}
add_filter( 'document_title_parts', 'bzg_filter_title', 10, 1 );function bzg_seo_title() {global $cat, $tag_id, $page, $paged;$page_num = '';if ( $paged >= 2 || $page >= 2 )$page_num = '_' . sprintf( '第%s頁', max( $paged, $page ) );$title = wp_get_document_title();if( is_author() )$title = '作者:' . $title;if( is_category() && get_term_meta( $cat , 'seo_title', true ) )$title = get_term_meta( $cat , 'seo_title', true );if( is_tag() && get_term_meta( $tag_id , 'seo_title', true ) )$title = get_term_meta( $tag_id , 'seo_title', true );if ( ! is_home() ) {$title .= $page_num . ' - ';$title .= get_option('blogname');} else {$description = get_option( 'blogdescription' );$home_title = get_option( 'home_title' );if ( $home_title ) {$title = $home_title;} elseif($description) {$title .= ' - ' . $description;}$title .= $page_num;}return $title;
}function bzg_seo_keywords() {global $post;$keywords = '';if ( is_home() )$keywords = get_option( 'home_keywords' );if ( ( is_category() || is_tag() ))$keywords = single_cat_title('', false);if ( is_single() || is_page() ) {if ( $post->post_excerpt ) {$keywords = $post->post_excerpt;} else {$keywords = $post->post_title;}}return $keywords;
}//Description標簽
function bzg_seo_description() {global $post;$description = '';if ( is_home() )$description = get_option( 'home_description' );if ( ( is_category() || is_tag() ) && category_description() )$description = wp_strip_all_tags( category_description(), true );if ( is_single() || is_page() ) {if ( $post->post_excerpt ) {$description = $post->post_excerpt;} else {$description = mb_strimwidth(esc_html(wp_strip_all_tags($post->post_content, true)), 0, 200);}}return $description;
}
header.php 調用:
<head><title><?php echo bzg_seo_title(); ?></title><?php$bzg_keywords = bzg_seo_keywords();if (!empty($bzg_keywords)) {echo '<meta name="keywords" content="' . $bzg_keywords . '" />';echo "\n";}$bzg_description = bzg_seo_description();if (!empty($bzg_description)) {echo '<meta name="description" content="' . $bzg_description . '" />';echo "\n";}?><?php if (is_home()) : ?><?php endif; ?></head>