IT服務網(wǎng)
wordpress自動添加meta keywords 和description關(guān)鍵詞和描述57
在主題的function里面添加 /*添加meta關(guān)鍵詞描述*/ add_action ( 'wp_head', 'wp_keywords' ); // 添加關(guān)鍵字 add_action ( 'wp_head', 'wp_description' ); // 添加頁面描述 /** +---------------------------------------------------------- * 站點關(guān)鍵字( +---------------------------------------------------------- * @return string +---------------------------------------------------------- */ function wp_keywords() { global $s, $post; $keywords = ''; if (is_single ()) { //如果是文章頁,關(guān)鍵詞則是:標簽+分類ID if (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 = '我是主頁關(guān)鍵詞'; //主頁關(guān)鍵詞設(shè)置 } elseif (is_tag ()) { //標簽頁關(guān)鍵詞設(shè)置 $keywords = single_tag_title ( '', false ); } elseif (is_category ()) {//分類頁關(guān)鍵詞設(shè)置 $keywords = single_cat_title ( '', false ); } elseif (is_search ()) {//搜索頁關(guān)鍵詞設(shè)置 $keywords = esc_HTML ( $s, 1 ); } else {//默認頁關(guān)鍵詞設(shè)置 $keywords = trim ( wp_title ( '', false ) ); } if ($keywords) { //輸出關(guān)鍵詞 echo "<meta name=\"keywords\" content=\"$keywords\" />\n"; } } /** +---------------------------------------------------------- * 站點描述 +---------------------------------------------------------- * @return string +---------------------------------------------------------- */ function wp_description() { global $s, $post; $description = ''; $blog_name = get_bloginfo ( 'name' ); if (is_singular ()) { //文章頁如果存在描述字段,則顯示描述,否則截取文章內(nèi)容 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 ()) {//首頁顯示描述設(shè)置 $description = $blog_name . "-" . get_bloginfo ( 'description' ) .'首頁要顯示的描述'; // 首頁要自己加 } elseif (is_tag ()) {//標簽頁顯示描述設(shè)置 $description = $blog_name . "有關(guān) '" . single_tag_title ( '', false ) . "' 的文章"; } elseif (is_category ()) {//分類頁顯示描述設(shè)置 $description = $blog_name . "有關(guān) '" . single_cat_title ( '', false ) . "' 的文章"; } elseif (is_archive ()) {//文檔頁顯示描述設(shè)置 $description = $blog_name . "在: '" . trim ( wp_title ( '', false ) ) . "' 的文章"; } elseif (is_search ()) {//搜索頁顯示描述設(shè)置 $description = $blog_name . ": '" . esc_html ( $s, 1 ) . "' 的搜索結(jié)果"; } else {//默認其他頁顯示描述設(shè)置 $description = $blog_name . "有關(guān) '" . trim ( wp_title ( '', false ) ) . "' 的文章"; } //輸出描述 $description = mb_substr ( $description, 0, 220, 'utf-8' ) . '..'; echo "<meta name=\"description\" content=\"$description\" />\n"; } /*自動設(shè)置第一張圖片為特色圖片,不支持外鏈圖片*/ function wp_autoset_featured_image() { global $post; if (!is_object($post)) return; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } } } add_action( 'the_post', 'wp_autoset_featured_image' ); add_action( 'save_post', 'wp_autoset_featured_image' ); add_action( 'draft_to_publish', 'wp_autoset_featured_image' ); add_action( 'new_to_publish', 'wp_autoset_featured_image' ); add_action( 'pending_to_publish', 'wp_autoset_featured_image' ); add_action( 'future_to_publish', 'wp_autoset_featured_image' ); |
關(guān)鍵詞列表
|