WordPress自动为文章添加标签(tags),加链接和控制数量

wordpress主题文章下方都会显示标签,文章tag标签不但可以为文章分类划分,也是有利于网站的SEO,总之tag标签用对了好处是很多的。平时我们发文章都需要手动选择设置tag标签,有些麻烦,下面BOBO就教大家用一段代码给WordPress新发布的文章自动添加标签。

 

标签(tags)

是另一种形式的分类方法,WordPress自动为文章添加tags标签原理是从文章或标题中提取与数据库相关的tags标签的词。

WordPress新发布的文章自动添加标签方法步骤:

  1. 登录自己WordPress网站的后台。
  2. 进入外观—>编辑—>模板函数functions.php文件。
  3. 将下面的给WordPress新发布的文章自动添加标签代码复制到functions.php文件即可。

一、根据文章内容关键词提取标签

// 自动为文章添加标签
add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
    $tags = get_tags( array('hide_empty' => false) );
    $post_id = get_the_ID();
    $post_content = get_post($post_id)->post_content;
    if ($tags) {
        foreach ( $tags as $tag ) {
            // 如果文章内容出现了已使用过的标签,自动添加这些标签
            if ( strpos($post_content, $tag->name) !== false)
                wp_set_post_tags( $post_id, $tag->name, true );
        }
    }
}

二、根据文章标题关键词提取标签

add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
$tags = get_tags( array('hide_empty' => false) );
$post_id = get_the_ID();
$post_title = get_post($post_id)->post_title;
if ($tags) {
foreach ( $tags as $tag ) {
// 如果文章标题出现了已使用过的标签,自动添加这些标签
if ( strpos($post_title, $tag->name) !== false)
wp_set_post_tags( $post_id, $tag->name, true );
}
}
}

三、根据文章内容关键词提取标签,加链接

$match_num_from = 1;        //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 1;      //一篇文章中同一个标签最多自动链接几次
function tag_sort($a, $b){
    if ( $a->name == $b->name ) return 0;
    return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tag_link($content){
    global $match_num_from,$match_num_to;
        $posttags = get_the_tags();
        if ($posttags) {
            usort($posttags, "tag_sort");
            foreach($posttags as $tag) {
                $link = get_tag_link($tag->term_id);
                $keyword = $tag->name;
                $cleankeyword = stripslashes($keyword);
                $url = "";
                $limit = rand($match_num_from,$match_num_to);
                $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case, '$1$2$4$5', $content);
                $content = preg_replace( '|()|U'.$case, '$1$2$4$5', $content);
                $cleankeyword = preg_quote($cleankeyword,'\'');
                $regEx = '\'(?!((<.*?)|(]*?)>)|([^>]*?))\'s' . $case;
                $content = preg_replace($regEx,$url,$content,$limit);
                $content = str_replace( '', stripslashes($ex_word), $content);
            }
        }
    return $content;
}
add_filter('the_content','tag_link',1);

注:这种方式只能对新增文章生效,旧文章要更新才可以。
//将大小写强行更改为标签的大小写
$content = str_ireplace(urldecode($tag->slug), $tag->name, $content);

全网热点精选

免插件实现WordPress文章自动添加标签

2021-9-11 23:34:14

全网热点精选

让Bing(必应)快速收录你的网站之新站收录SEO的捷径

2021-9-14 9:59:11

重要说明

本站资源大多来自网络,如有侵犯你的权益请联系管理员 邮箱:baiddiz221@gmail.com  或给邮箱发送邮件welnn@outlook.com 我们会第一时间进行审核删除。 站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果你遇到支付完成,找不到下载链接,或者不能下载,或者解压失败,先不要忙,提交工单,我们会尽快处理 (博主有可能有事情或者在睡觉不能及时的回复您,邮箱留言后,请耐心等待即可!)

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索