0°

wordpress页面的description和keywords根据不同页面自动获取

wordpress页面的description和keywords根据不同页面自动获取以下为代码,放入主题下header.php页面的title下面.


//首页
<?if (is_home()){
    $description = "首页描述";
    $keywords = "关键词";
}
	//文章页
	elseif (is_single()){
    if ($post->post_excerpt) {
        $description = $post->post_excerpt;
    } else {
		$description = mb_strimwidth(strip_tags(
		apply_filters('the_content',$post->post_content)
		),0,220);
        //以下为排除回车换行、空格等符号,如有需要替换上面三行即可.
	//$description = mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 250);
        //$description = str_replace("\n", "", $description);
        //$description = str_replace(" ", "", $description);
        //$description = str_replace("\t", " ", $description);
        //$description = str_replace("\"", "", $description);
    }

    $keywords = "";
    $tags = wp_get_post_tags($post->ID);
    foreach ($tags as $tag ) {
        if ("" != $keywords) $keywords .= ",";
        $keywords = $keywords . $tag->name;
    }
}
	//分类
	elseif (is_category()) {
        $description = category_description();
        $keywords = single_cat_title( '', false );
	//以下为排除回车换行、空格等符号,不需要可直接删除.
        //$description = str_replace("<p>", "", $description);
        //$description = str_replace("</p>", "", $description);
        //$description = str_replace("\n", "", $description);
}
?>
<meta name="keywords" content="<?=$keywords?>" />
<meta name="description" content="<?=$description?>" />
「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论