WordPress主题网站页面添加描述和关键词 php代码 包含搜索页 页面

最近在写wordpress 主题,然后再网上找回来的代码发现,没有搜索页及普通页面的 关键词及描述。下面我们对网上的代码进行补充。代码如下:

<title><? wp_title( '_', true, 'right' ); ?></title>
<? 
if (is_home()){  
	$keywords = $description = get_bloginfo('description'); 
} elseif (is_single()){  
	$tags = wp_get_post_tags($post->ID);  
	foreach ($tags as $tag){   
		$keywords = $keywords.$tag->name.",";  
	}  
	$keywords = rtrim($keywords, ', ');  
	if($post->post_excerpt){   
		$description = $post->post_excerpt;  
	}else{   
		$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200);  
	} 
} elseif (is_page()){  
	$keywords = get_post_meta($post->ID, "keywords", true);  
	$description = get_post_meta($post->ID, "description", true); 
} elseif (is_category()){  
	$keywords = single_cat_title('', false);  
	$description = category_description(); 
} elseif (is_tag()){  
	$keywords = single_tag_title('', false);  
	$description = tag_description(); 
} elseif(is_search()) {
    $keywords = $s;
    $description = "Search result of ".$s;
}
$keywords = trim(strip_tags($keywords)); 
$description = trim(strip_tags($description)); 
?> 
<meta name="keywords" content="<? echo $keywords; ?>" /> 
<meta name="description" content="<? echo $description; ?>" />
<link rel="canonical" href="<? echo get_permalink($post->ID);?>" />

欢迎各位大神做补充~

搬瓦工,年付$49.99CN2高速线路,1024MB内存/1000GB流量/1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr月付$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

发表评论

电子邮件地址不会被公开。 必填项已用*标注