wordpress免插件实现TAG Category自动添加链接

作者:matrix 被围观: 3,000 次 发布时间:2014-01-29 分类:Wordpress 零零星星 | 7 条评论 »

这是一个创建于 3738 天前的主题,其中的信息可能已经有所发展或是发生改变。

此功能可以由WP keyword Link Plugin插件实现的,不过要非插件化只有另找。

网上一大把代码我这都不能用。不知道为何。

豆腐君扒的代码,真心没法用。幸好懂点正则。自己慢慢改。

改的时候发现网上的代码WP keyword Link Plugin插件的wp_keywordlink.php部分有9成相似。参照wp_keywordlink.php那该好多了。

代码:

/** 
 * TAG Category自动添加链接 by 不懂. 20140129 修改 
 */  
add_filter('the_content', 'Category_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 Category_tag_link($content)  
{  
    /** 
     * --------------------------------------配置处-------------------------------------------- 
     */  
    $match_num_from = 1; //配置:一个关键字少于多少不替换   
    $match_num_to = 2; //配置:一个关键字最多替换,建议不大于2  
    $case = true ? "i" : ""; //配置:忽略大小写 true是开,false是关  
    $get_the_category=is_array(get_the_category())?get_the_category():array();  
    $get_the_tags=is_array(get_the_tags())?get_the_tags():array();  
    $posttags = array_merge($get_the_tags, $get_the_category); //合并TAG & CAT数组 (array)强制转换数组,防止报错  
    if ($posttags)  
    {  
        usort($posttags, "tag_sort"); //重新排序 回调函数tag_sort  
        foreach($posttags as $tag)  
        {  
            $link = $tag->category_count ? esc_url(get_category_link($tag->term_id)) : esc_url(get_tag_link($tag->term_id)); //TAG & CAT 合并URL  
            $keyword = $tag->name; //TAG name  
            $cleankeyword = stripslashes($keyword);  
            $url = "<a href=\"$link\" title=\"" . str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')) . "\""; //查看 %s 中的全部文章。__()函数WordPress本地化翻译。  
            $url .= 'target="_blank"';  
            $url .= ">" . addcslashes($cleankeyword, '$') . "</a>";  
            $limit = rand($match_num_from, $match_num_to);  
            $ex_word = preg_quote($cleankeyword, '\'');  
            $content = preg_replace("'(<a[^>]+>)(.*)($ex_word)(.*)(</a[^>]*>)'U" . $case, '$1$2*&%*$4$5', $content); //a标签,免混淆处理  
            $content = preg_replace('|(<img)(.*?)(' . $ex_word . ')(.*?)(>)|U' . $case, '$1$2*&%*$4$5', $content); //img标签  
            $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;  
}  

说明:代码放到WordPress主题functions.php文件的?>前面。

配置信息在15-17行处。

此版本增加Category(文章分类)链接,忽略大小写功能。比网上传的好点。哈哈。ok  丢掉WP keyword Link Plugin

示例见本站任意文章页面。或者这里:免插件

不方便copy的php下载地址:

http://www.400gb.com/file/55854122

折腾完觉得正则真TM牛逼。好菜鸟啊


记:

遇到Warning : preg_replace()  [function.preg-replace ]: Unknown  modifier 'a'这类问题。实质是正则的边界符没弄好的缘故。

一般的边界符号是用 | 或者 /,是在开头和结尾出现的。然而正则表达式里也出现了边界符,系统会把它当做边界,这样边界后面出现的以a开头的不明字符串就会成为正则修正符,自然是不会别识别的。也就导致报错。

错误例:

$content = preg_replace("/(<a[^>]+>)(.)($ex_word)(.)(</a[^>]>)/U" . $case, '$1$2&%*$4$5', $content);

改成:/(<a[^>]+>)(.)($ex_word)(.)(<\/a[^>]*>)/U

|(<a[^>]+>)(.)($ex_word)(.)(</a[^>]*>)|U

'(<a[^>]+>)(.)($ex_word)(.)(</a[^>]*>)'U

都ok啦。边界符也不是固定的,'、"照样可以用。

Warning: array_merge() [function.array-merge]: Argument #1报错, array_merge()的参数不是数组就会导致此类ERROR。

可在参数前面加(array)来强制转换为数组,建议在 array_merge() 前判断是否为数组,否则以空数组输出到array_merge()中解决。

正则入门级教程:http://www.oschina.net/question/12_9507  很实用的~

正则表达式 问号 冒号 ?:使用 http://blog.csdn.net/hoping23/article/details/8479700

php正则表达式中的修正符说明:http://blog.csdn.net/taipingliebeiluo/article/details/5872878

WordPress中的()和_e()函数的作用:http://demon.tw/software/WordPress--_e.html

零宽断言:http://jjdoor.blog.163.com/blog/static/184780342012318917389/

参考:http://bbs.csdn.net/topics/90492431

http://blog.csdn.net/sunking18/article/details/6415705

其他文章:
本文固定链接:https://www.hhtjim.com/free-wordpress-plugin-implementation-tag-category-automatically-add-links.html
matrix
本文章由 matrix 于2014年01月29日发布在Wordpress, 零零星星分类下,目前没有通告,你可以至底部留下评论。
转载请注明:wordpress免插件实现TAG Category自动添加链接-HHTjim'S 部落格
关键字:, , ,

有7 条评论 »

  1. WordPress主题 WordPress主题 2014-2-8 16:00:17 +0800#3

    好吧,以前都是手动添加的,以后还是。哈哈 😆

    • Matrix Matrix Moderator 2014-2-8 16:34:09 +0800

      还有你这样的。 😆 看我这样多爽

  2. 逗妇乳 逗妇乳 2014-2-4 13:28:17 +0800#2

    你这正则把我的图片给正处事来了

    • Matrix Matrix Moderator 2014-2-4 16:01:25 +0800

      看来主题上兼容的好差。 删掉就是,影响不大。

  3. 手机小菜 手机小菜 2014-1-31 9:42:26 +0800#1

    支持个,我是以前在别人主题里扒的,和你这个不太一样,正则不懂

    • Matrix Matrix Moderator 2014-1-31 13:34:16 +0800

      我看这个才学会一点的:http://www.oschina.net/question/12_9507 很实用~

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!