WP酷 WP酷
  • 主题
  • 教程
  • 插件
  • 官方 QQ 群
  • 建站推荐
  • 联系
  • nicetheme® 奈思主题
  • 注册
    登录
立即登录
  • 请到 [后台->外观->菜单] 中设置菜单
首页 › WordPress 教程 › 抛弃Auto Tags Link:不用插件实现关键词自动添加链接

抛弃Auto Tags Link:不用插件实现关键词自动添加链接

PCDotFan9年前

抛弃Auto Tags Link:不用插件实现关键词自动添加链接-WP酷

这是比较老的一个技巧了,网上的资料显示文章内的关键词如果加上链接是有利于搜索引擎更好地收录的。比较著名的插件有Auto Tags Link,只为实现一个功能而动用插件太大材小用了,在functions.php中添加如下代码:

//连接数量
$match_num_from = 1; //一个关键字少于多少不替换
$match_num_to = 5; //一个关键字最多替换
//连接到WordPress的模块
add_filter('the_content','tag_link',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 = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\"";
$url .= 'target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
//不连接的代码
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}

 

#auto tags link
3
分享

本站下载管理系统「dl.mywpku.com」因年久失修而永久关闭。若需获得往年主题,请至 WP酷的百度网盘(提取码: 5rbm) 自行搜索下载。默认解压密码为「www.mywpku.com」。

对您造成的不便,敬请谅解!

PCDotFan 站长
文章 435评论 859
相关文章
  • [6.0 更新中文汉化]极度流行的SEO插件 – WordPress SEO by Yoast
  • WordPress 新用户注册邮件链接提示「您的密码重设链接无效」
  • AMP – 为 WordPress 移动站点操作全球访问优化
  • 去除谷歌服务 – WordPress 国外主题全方位加速
  • 使用 WP-CLI 简化 WordPress 操作流程
  • WordPress 基础开发 – 一探 WordPress Hook 机制及示例应用
  • 优化静态资源 – WordPress 国外主题全方位加速
  • WordPress 实现微信登录
评论 (6)
再想想
  • zeroten

    这个对于中文关键字正常么

    9年前
    • WP酷

      @zeroten 这个与中文关键字没有关系,任何语言都是可以的

      9年前
  • imj2

    如何实现全站文章内容出现已存在的tag自动链接?

    8年前
  • 乐朦

    出现两个错误提示:
    Notice: Undefined variable: ex_word in
    Notice: Undefined variable: case in
    不懂php,不知道怎么搞,请指教

    6年前
  • 靠谱网赚

    拿走试试。

    2年前
PCDotFan
站长
若无特别注明,则默认我发布的所有文章均为内容原创 / 翻译原创,转载时请保留来源。
435文章
859评论
3K获赞
猜你喜欢
可否与垃圾评论说再见?Willin Kan之Anti-Spam版本集合
9年前
WordPress 获取文章的评论人数
8年前
WordPress 教程集合 – 打造最详细的WP后台教程【持续更新】
8年前
  • 杂记
  • WordPress 付费主题推荐
Copyright © 2022 WP酷. Designed by nicetheme. 琼ICP备13002067号-1