最新公告
  • 欢迎您光临65源码网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • WordPress自动内链标签及自动内链关键字

    将以下代码添加到当前主题的functions.php文件中保存:

    /* 自动为文章内的标签添加内链开始 */
    $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 = "<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;
    }
    add_filter('the_content','tag_link',1);
    /* 自动为文章内的标签添加内链结束 *
    

    代码中注释的很清楚,可以自行改正。
    二、指定关键词自动内链
    将以下代码添加到当前主题的functions.php文件中保存:

    
    /* 文章指定关键词添加内链开始 */
    function replace_text_wps($text){
            $replace = array(
                    '65源码网' => '<a href="https://www.65ymw.com/" target="_blank" title="65源码网">65源码网</a>',
                    '65源码网' => '<a href="https://www.65ymw.com/" target="_blank" title="65源码网">65源码网</a>',
                    '教程' => '<a href="https://www.ztmbk.com/jiaocheng/" target="_blank" title="教程">教程</a>'
            );
            $text = str_replace(array_keys($replace), $replace, $text);
            return $text;
    }
         
    add_filter('the_content', 'replace_text_wps');
    

    大家自行把代码中的关键词与链接换成自己的,需要其他关键词复制那一行代码即可,非常方便,这个方法有个缺陷就是不能控制关键词锚文本次数,只要在文章中出现的关键词不管几次都会加上链接

    免责声明 1 本网站名称:65源码网
    2 本站永久网址:http://www.65ymw.com
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长 QQ34454443进行删除处理。
    4 所有资源仅供大家学习和交流,请不要用于商业及违法用途,有此产生的后果与本站无关!
    5 如果你有闲置源码或教程,可以到个人中心投稿区发布,有金币奖励和额外收入!
    6 本站提供的源码、模板、插件等其它资源,都不包含技术服务请大家谅解!
    7 如有链接无法下载、失效或广告,请联系网站客服处理!
    8 本站资源售价只是赞助,收取的费用仅用于维持本站的日常运营!会员及资源为虚拟物品复制性强不支持任何形式的退款!
    9 如遇到加密压缩包,默认解压密码为"65ymw.cn",如遇到无法解压的请联系客服!
    10如遇到支付或充值失败或充值未到账不要急!略有延时2小时内人工补单均能到帐特殊情况除外。

    65源码网 » WordPress自动内链标签及自动内链关键字

    发表评论

    • 939会员总数(位)
    • 44405资源总数(个)
    • 253本周发布(个)
    • 45 今日发布(个)
    • 1803稳定运行(天)

    提供最优质的资源集合

    立即查看 了解详情
    升级SVIP尊享更多特权立即升级