WordPress站长对文章别名很熟悉,国外很多站点的固定链接都是采用文章别名的。伪静态设置了%postname% 这样的别名固定链接才能使用,而中文站点自动生成的文章别名直接是汉字,还需要我们自动修改, WordPress 文章别名设置为文章 ID、英文、拼音完全教程。

WordPress教程-文章链接优化中文Url教程-痴痴资源网

将文章别名设置为文章 ID

1、新文章自动使用 ID 作为别名

将下面的代码添加到主题的 functions.php,新建的文章都会自动使用 ID 作为别名

<span class="token comment">/**
* WordPress 新文章自动使用 ID 作为别名
* 痴痴资源网 www.chichisvip.com
*/</span>
<span class="token function">add_action</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'save_post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'using_id_as_slug'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function-definition function">using_id_as_slug</span><span class="token punctuation">(</span><span class="token variable">$post_id</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">global</span> <span class="token variable">$post_type</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$post_type</span><span class="token operator">==</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//只对文章生效</span>
<span class="token comment">// 如果是文章的版本,不生效</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">wp_is_post_revision</span><span class="token punctuation">(</span><span class="token variable">$post_id</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span>
<span class="token comment">// 取消挂载该函数,防止无限循环</span>
<span class="token function">remove_action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'save_post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'using_id_as_slug'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 使用文章 ID 作为文章的别名</span>
<span class="token function">wp_update_post</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ID'</span> <span class="token operator">=&gt;</span> <span class="token variable">$post_id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'post_name'</span> <span class="token operator">=&gt;</span> <span class="token variable">$post_id</span> <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 重新挂载该函数</span>
<span class="token function">add_action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'save_post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'using_id_as_slug'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}
</span>

2、修改老文章别名为文章 ID

将下面的代码添加到主题的 functions.php

<span class="token comment">/**
* 修改 WordPress 旧文章别名为文章 ID
* 痴痴资源网 www.chichisvip.com
*/</span>
<span class="token keyword">function</span> <span class="token function-definition function">Bing_post_name_id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token function">query_posts</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'posts_per_page=-1'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span> <span class="token function">have_posts</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token function">the_post</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$post_id</span> <span class="token operator">=</span> <span class="token variable">$GLOBALS</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">]</span><span class="token operator">-&gt;</span><span class="token constant">ID</span><span class="token punctuation">;</span>
<span class="token function">wp_update_post</span><span class="token punctuation">(</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'ID'</span> <span class="token operator">=&gt;</span> <span class="token variable">$post_id</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'post_name'</span> <span class="token operator">=&gt;</span> <span class="token variable">$post_id</span>
<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">wp_reset_query</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">if</span><span class="token punctuation">(</span> <span class="token variable">$_GET</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'post_name_id'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'yes'</span> <span class="token punctuation">)</span> <span class="token function">add_action</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'init'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Bing_post_name_id'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

放置好代码后,访问一下下边的网站,访问前注意一定要备份数据,以免出错。
http://xxxx.com?post_name_id=yes
文章多的话网页加载会比较慢,因为正在一篇文章一篇文章的修改别名,要耐心等到网页完全加载好才可以。
加载好后,删除之前添加的代码,修改完成,所有文章的别名都会变成自身的 ID。

将文章别名设置为标题英文

<span class="token comment">// 自动生成英文别名 - 百度版</span>
<span class="token keyword">function</span> <span class="token function-definition function">bymt_slug_auto_translate</span><span class="token punctuation">(</span> <span class="token variable">$title</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$wp_http_get</span> <span class="token operator">=</span> <span class="token function">wp_safe_remote_get</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'http://fanyi.baidu.com/v2transapi?from=zh&amp;to=en&amp;transtype=trans&amp;query='</span> <span class="token operator">.</span> <span class="token variable">$title</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 function">emptyempty</span><span class="token punctuation">(</span> <span class="token variable">$wp_http_get</span><span class="token operator">-&gt;</span><span class="token property">errors</span> <span class="token punctuation">)</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 operator">!</span> <span class="token function">emptyempty</span><span class="token punctuation">(</span> <span class="token variable">$wp_http_get</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'body'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$trans_result</span> <span class="token operator">=</span> <span class="token function">json_decode</span><span class="token punctuation">(</span> <span class="token variable">$wp_http_get</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'body'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$trans_title</span> <span class="token operator">=</span> <span class="token variable">$trans_result</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'trans_result'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'dst'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$trans_title</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 punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'sanitize_title'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bymt_slug_auto_translate'</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">// 自动生成英文别名 - 微软版</span>
<span class="token keyword">function</span> <span class="token function-definition function">bymt_slug_auto_translate</span><span class="token punctuation">(</span> <span class="token variable">$title</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$wp_http_get</span> <span class="token operator">=</span> <span class="token function">wp_safe_remote_get</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'http://api.microsofttranslator.com/v2/ajax.svc/TranslateArray2?appId=%22TKT68kjRgkUbVtIKst6Vo0Hxnb6g2f0K3tUMyn1gZ7nc*%22&amp;from="zh"&amp;to="en"&amp;options={}&amp;texts=["'</span> <span class="token operator">.</span> <span class="token variable">$title</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'"]'</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 function">emptyempty</span><span class="token punctuation">(</span> <span class="token variable">$wp_http_get</span><span class="token operator">-&gt;</span><span class="token property">errors</span> <span class="token punctuation">)</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 operator">!</span> <span class="token function">emptyempty</span><span class="token punctuation">(</span> <span class="token variable">$wp_http_get</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'body'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">preg_match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/"TranslatedText":"([^"]+)/i'</span><span class="token punctuation">,</span> <span class="token variable">$wp_http_get</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'body'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$trans_result</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$trans_title</span> <span class="token operator">=</span> <span class="token variable">$trans_result</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 keyword">return</span> <span class="token variable">$trans_title</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 punctuation">;</span>
<span class="token punctuation">}</span>

小逸在实际测试中发现,使用此方法设置英文别名速度略慢,毕竟是是查询翻译结果,且翻译出来的别名也较长,不太推荐。

将文章别名设置为标题拼音

以下分享几个较好的插件,可以自动将别名设置为标题的拼音:

Pinyin SEO
这一款是国人开发的,在国内使用量也是较多的一款,可以自动把文章页,分类页,标签页的中文别名自动转换成为拼音格式,还可以把以前文章中的别名也转换过来,使用这款插件的效率是比较高的。

wp slug translate
这款插件貌似已经不再更新了,插件的功能相当的强大,可以把一切中文别名转化为英文或者是拼音,包括文章,分类,标签页等等,但需要主机开启 fsockopen 函数,否则则不能正常使用,如果没有主机权限的小伙伴就不要使用了。

Auto Tag Slug
这一款插件主要是针对标签 tag 来自动转换的,将 tag 中文别名转换成为拼音或者是英文,可以进行批量转换,同样需要开启 fsockopen 函数,否则会出现错误提示:Function fsockopen() is not supported, please contact your webmaster.自我感觉安装这款插件的意义不大,如果一个个人博客站点,tag 是非常有限的,不会有太大,我们完全可以手动去转换。

文/转载:www.scczz.com/191.html

常见问题FAQ

部分免费版权图片来自www.freepik.com
本站部分免费版权图片由 Freepik 设计www.freepik.com!!
本站资源和文章版权声明
版权声明:本站部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们,减少损失。联系邮箱:1732765629@qq.com。
购买商品请必看特别声明
所有资源请勿商业运营,违法使用和传播!仅研究学习使用!下载后请在24小时内删除!本站资源大多来自互联网整合,未测试,未标有售后默认无售后,可能会有瑕疵或BUG,介意勿下载!建议注册账号下载。资源付的费为整理辛苦费,资源本身不收费。常见问题解答:点我直达!有问题请反馈:1732765629@qq.com ,感谢支持!
免费下载或者贵宾会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 《免责声明》。
点击下载链接不跳转怎么回事?
您好,QQ内打开是不跳转的,一少部分浏览器也不跳转,一般的UC了,搜狗 360等浏览器都可以跳转。
立即查看