WordPress博客添加首页、文章页、页面、分类页、标签页的关键字和描述

用编辑器打开主题的header.php,在<head>与</head>之间任意地方(一般加到</title>下面比较美观)加上如下代码:

<?php
//如果是首页
if (is_home()){
    $keywords = "你网站首页的关键字,自己修改吧";
    $description = "你网站首页的描述,自己修改吧";
}
//如果是文章页
elseif (is_single()){
    //默认使用文章页添加关键字
    $keywords = get_post_meta($post->ID, "keywords", true);
    //如果为空,使用标签作为关键字
    if($keywords == ""){
        $tags = wp_get_post_tags($post->ID);
        foreach ($tags as $tag){
            $keywords = $keywords.$tag->name.",";
        }
        //去掉最后一个,
        $keywords = rtrim($keywords, ', ');
    }
    //默认使用文章页添加描述
    $description = get_post_meta($post->ID, "description", true);
    //如果为空,使用文章前100个字作为描述
    if($description == ""){
        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();
}
//去掉两段空格
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />

 
这次把主要的注释都加上了,我想大家应该看得懂吧?主要是文章页那里多了几个判断。默认使用文章页添加关键字,如果为空,使用标签作为关键字。默认使用文章页添加描述,如果为空,使用摘要内容,如果摘要为空,使用文章前100个字作为描述。

如果你看到了这里,那么我相信一定会有人觉得又要下载PHP文件,又要改functions.php好麻烦啊!而且也不愿意手动加这些信息,都用自动的。好吧,下面公布最懒的办法。

这个办法只需要修改一次代码,用编辑器打开header.php,在<head>与</head>之间任意地方(一般加到<title>下面比较美观)加上如下代码:

<?php
if (is_home()){
    $keywords = "你网站首页的关键字,自己修改吧";
    $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();
}
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />

 5个页面的关键字和描述,设置一次,以后全部都是自动的。

转载于:https://my.oschina.net/ajian2014/blog/306639

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值