以下代码实现的是以标签为关键词;以摘要为描述,如果没有填写摘要,那就自动截取文章前200字为描述。(教程转自:DeveWork)
代码实现WordPress自动关键词与描述:
以下代码放到你的主题下funtions.php的最后一个 ?>前:
- function get_cats_name() {
- $allcats=get_categories();
- foreach ($allcats as $category)
- {
- $keywords[] = $category->cat_name;
- }
- return $keywords;
- }
- function utf8Substr($str, $from, $len) {
- return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.
- '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',
- '$1',$str);
- }
- function meta_SEO() {
- global $post;
- $output = '';
- if (is_single()){
- $keywords = '';
- $description = '';
- if ($post->post_excerpt) {
- $description = $post->post_excerpt;
- $description = str_replace("rn","",$description);
- $description = str_replace("n","",$description);
- $description = str_replace(""","'",$description);
- $description .= '...';
- } else {
- $description = utf8Substr(strip_tags($post->post_content),0,200);
- $description = str_replace("rn","",$description);
- $description = str_replace("n","",$description);
- $description = str_replace(""","'",$description);
- $description .= '...';
- }
- $tags = wp_get_post_tags($post->ID);
- foreach ($tags as $tag ) {
- $keywordarray[] = $tag->name;
- }
- $keywords = implode(',',array_unique((array)$keywordarray));
- } else {
- $keywords = 'wordpress,wordpress主题,wordpress教程,wordpress主题下载,wordpress博客主题,wordpress企业主题,wordpress主题定制';
- $description = '主题猫,致力于为广大网友提供最新最全的wordpress主题';
- }
- $output .= '<meta name="keywords" content="' . $keywords . '" />' . "n";
- $output .= '<meta name="description" content="' . $description . '" />' . "n";
- echo "$outputn";
- }
- add_action('wp_head', 'meta_SEO');
第43行与第44行的内容需要根据你的网站进行修改。