WordPress无插件自动实现tag关键字内链,抛弃wp-keyword-link插件



之前一直使用WP keyword link这个插件给文章中的关键字增加内链,以优化SEO。但插件多了对博客的速度肯定有影响,所以找了个无插件实现的方法。实现起来也很简单,只需在主题文件夹中的function.php最后一个?>之前增加以下代码即可实现tag自动内链:

<span class="token comment" spellcheck="true">//tag自动内链
</span><span class="token variable">$match_num_from</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> //一篇文章中同一个关键字少于多少不锚文本(这个直接填1就好了)
</span><span class="token variable">$match_num_to</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true"> //一篇文章中同一个关键字最多出现多少次锚文本(建议不超过2次)
</span><span class="token comment" spellcheck="true">//连接到WordPress的模块
</span><span class="token function">add_filter<span class="token punctuation">(</span></span><span class="token string">'the_content'</span><span class="token punctuation">,</span><span class="token string">'tag_link'</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">
//按长度排序
</span><span class="token keyword">function</span> <span class="token function">tag_sort<span class="token punctuation">(</span></span><span class="token variable">$a</span><span class="token punctuation">,</span> <span class="token variable">$b</span><span class="token punctuation">)</span><span class="token punctuation">{</span>	<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token variable">$a</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span> <span class="token operator">==</span> <span class="token variable">$b</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span> <span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>	<span class="token keyword">return</span> <span class="token punctuation">(</span> <span class="token function">strlen<span class="token punctuation">(</span></span><span class="token variable">$a</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token function">strlen<span class="token punctuation">(</span></span><span class="token variable">$b</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token comment" spellcheck="true">
//改变标签关键字
</span><span class="token keyword">function</span> <span class="token function">tag_link<span class="token punctuation">(</span></span><span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">global</span> <span class="token variable">$match_num_from</span><span class="token punctuation">,</span><span class="token variable">$match_num_to</span><span class="token punctuation">;</span> <span class="token variable">$posttags</span> <span class="token operator">=</span> <span class="token function">get_the_tags<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$posttags</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">usort<span class="token punctuation">(</span></span><span class="token variable">$posttags</span><span class="token punctuation">,</span> <span class="token string">"tag_sort"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">foreach</span><span class="token punctuation">(</span><span class="token variable">$posttags</span> <span class="token keyword">as</span> <span class="token variable">$tag</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$link</span> <span class="token operator">=</span> <span class="token function">get_tag_link<span class="token punctuation">(</span></span><span class="token variable">$tag</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">term_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$keyword</span> <span class="token operator">=</span> <span class="token variable">$tag</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span><span class="token punctuation">;</span>	<span class="token comment" spellcheck="true"> //连接代码
</span> <span class="token variable">$cleankeyword</span> <span class="token operator">=</span> <span class="token function">stripslashes<span class="token punctuation">(</span></span><span class="token variable">$keyword</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">.</span><span class="token function">addcslashes<span class="token punctuation">(</span></span><span class="token variable">$cleankeyword</span><span class="token punctuation">,</span> <span class="token string">'$'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token string">""</span><span class="token punctuation">;</span> <span class="token variable">$limit</span> <span class="token operator">=</span> <span class="token function">rand<span class="token punctuation">(</span></span><span class="token variable">$match_num_from</span><span class="token punctuation">,</span><span class="token variable">$match_num_to</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment" spellcheck="true">	//不连接的代码
</span> <span class="token variable">$content</span> <span class="token operator">=</span> <span class="token function">preg_replace<span class="token punctuation">(</span></span> <span class="token string">'|(]+&gt;)(.*)('</span><span class="token punctuation">.</span><span class="token variable">$ex_word</span><span class="token punctuation">.</span><span class="token string">')(.*)(]*&gt;)|U'</span><span class="token punctuation">.</span><span class="token variable">$case</span><span class="token punctuation">,</span> <span class="token string">'$1$2%&amp;&amp;&amp;&amp;&amp; %$4$5'</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$content</span> <span class="token operator">=</span> <span class="token function">preg_replace<span class="token punctuation">(</span></span> <span class="token string">'|()|U'</span><span class="token punctuation">.</span><span class="token variable">$case</span><span class="token punctuation">,</span> <span class="token string">'$1$2%&amp;&amp;&amp;&amp;&amp; %$4$5'</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token variable">$cleankeyword</span> <span class="token operator">=</span> <span class="token function">preg_quote<span class="token punctuation">(</span></span><span class="token variable">$cleankeyword</span><span class="token punctuation">,</span><span class="token string">'\''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token variable">$regEx</span> <span class="token operator">=</span> <span class="token string">'\'</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">!</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token markup">&lt;.*?)|(]*?)&gt;</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">^</span><span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token operator">?</span><span class="token punctuation">)</span><span class="token punctuation">)</span>\<span class="token string">'s'</span> <span class="token punctuation">.</span> <span class="token variable">$case</span><span class="token punctuation">;</span>	<span class="token variable">$content</span> <span class="token operator">=</span> <span class="token function">preg_replace<span class="token punctuation">(</span></span><span class="token variable">$regEx</span><span class="token punctuation">,</span><span class="token variable">$url</span><span class="token punctuation">,</span><span class="token variable">$content</span><span class="token punctuation">,</span><span class="token variable">$limit</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token variable">$content</span> <span class="token operator">=</span> <span class="token function">str_replace<span class="token punctuation">(</span></span> <span class="token string">'%&amp;&amp;&amp;&amp;&amp; %'</span><span class="token punctuation">,</span> <span class="token function">stripslashes<span class="token punctuation">(</span></span><span class="token variable">$ex_word</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token variable">$content</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

更新你的博客看看,是不是已经有效果了。

本文由 冰风BOKE 作者:冰风 发表,其版权均为 冰风BOKE 所有,文章内容系作者个人观点,不代表 冰风BOKE 对观点赞同或支持。如需转载,请注明文章来源。
0

发表评论