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

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

<title><?php wp_title( '_', true, 'right' ); ?></title>
<?php 
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="<?php echo $keywords; ?>" /> 
<meta name="description" content="<?php echo $description; ?>" />
<link rel="canonical" href="<?php echo get_permalink($post->ID);?>" />

欢迎各位大神做补充~

链接到文章: https://ixvps.com/38666.html

发表回复

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