炫意html5
最早CSS3和HTML5移动技术网站之一

自定义WordPress里标签(tag)和分类目录(category)的固定链接结构的方法

对于Wordpress比较熟悉的朋友应该知道,如果想修改文章(post)的固定链接结构(permalinks),可以到Wordpress后台管理界面进行修改,比如说,如果你想让页面地址都有一个“news”的前缀,形如:

http://www.xyhtml5.com/news/hollow-world/

你可以在后台的“固定链接”设置界面将固定链接结构设置为“/news/%postname%”就行了。

但是,你会发现,只有文章页(post)的链接结构上这样改的,而一般页面(page)的链接结构没有变化,而且,标签(tag)的链接结构也变成了”/news/%tag%”样子。

如果你想让所有的页面(page)的固定链接结构也变成“/news/%pagename%”模式,之前的文章《修改WordPress里页面(page)的固定链接结构的方法》里清楚的介绍了修改的方法。

现在的问题是,如果你想自定义标签(tag)的固定链接,让它不随文章页(post)的链接结构的变化而变化,让标签页(tag)具有更个性的链接结构,怎么办呢?

或者,我还想自定义分类目录页(category)的固定链接结构,有什么办法?

从《修改WordPress里页面(page)的固定链接结构的方法》里可以看出,我们是应该从$wp_rewrite 对象下手。前提是要知道 $wp_rewrite 对象里数据结构究竟是怎样的,才能找出修改的方法,于是我把 $wp_rewrite 打印了出来,下面就是这个对象的内部结构:

WP_Rewrite Object
(
[permalink_structure] => /post/%postname%.html
[use_trailing_slashes] =>
[author_base] => author
[page_structure] => %pagename%.html
[search_base] => search
[comments_base] => comments
[pagination_base] => page
[comments_pagination_base] => comment-page
[feed_base] => feed
[front] => /post/
[root] =>
[index] => index.php
[matches] =>
[rules] =>
[extra_rules] => Array
(
)
[extra_rules_top] => Array
(
)
[non_wp_rules] => Array
(
)
[extra_permastructs] => Array
(
[category] => Array
(
[with_front] =>
[ep_mask] => 512
[paged] => 1
[feed] => 1
[forcomments] =>
[walk_dirs] => 1
[endpoints] => 1
[struct] => cat/%category%
)
[post_tag] => Array
(
[with_front] =>
[ep_mask] => 1024
[paged] => 1
[feed] => 1
[forcomments] =>
[walk_dirs] => 1
[endpoints] => 1
[struct] => tag/%post_tag%
)
[post_format] => Array
(
[with_front] => 1
[ep_mask] => 0
[paged] => 1
[feed] => 1
[forcomments] =>
[walk_dirs] => 1
[endpoints] => 1
[struct] => /post/type/%post_format%
)
)
[endpoints] => Array
(
)
[use_verbose_rules] =>
[use_verbose_page_rules] => 1
[rewritecode] => Array
(
[0] => %year%
[1] => %monthnum%
[2] => %day%
[3] => %hour%
[4] => %minute%
[5] => %second%
[6] => %postname%
[7] => %post_id%
[8] => %author%
[9] => %pagename%
[10] => %search%
[11] => %category%
[12] => %post_tag%
[13] => %post_format%
)
[rewritereplace] => Array
(
[0] => ([0-9]{4})
[1] => ([0-9]{1,2})
[2] => ([0-9]{1,2})
[3] => ([0-9]{1,2})
[4] => ([0-9]{1,2})
[5] => ([0-9]{1,2})
[6] => ([^/]+)
[7] => ([0-9]+)
[8] => ([^/]+)
[9] => ([^/]+?)
[10] => (.+)
[11] => (.+?)
[12] => ([^/]+)
[13] => ([^/]+)
)
[queryreplace] => Array
(
[0] => year=
[1] => monthnum=
[2] => day=
[3] => hour=
[4] => minute=
[5] => second=
[6] => name=
[7] => p=
[8] => author_name=
[9] => pagename=
[10] => s=
[11] => category_name=
[12] => tag=
[13] => post_format=
)
[feeds] => Array
(
[0] => feed
[1] => rdf
[2] => rss
[3] => rss2
[4] => atom
)
)

这下就清楚了,之前我们修改页面(page)的固定链接结构是修改了[page_structure] => %pagename%.html,而修改标签页(tag)的固定链接结构很显然是要修改[extra_permastructs][post_tag][struct] => tag/%post_tag%这个值,而修改分类目录页(category)的固定链接,自然是修改[extra_permastructs][category][struct] => cat/%category%的值。

下面是具体的修改代码:

function custom_page_rules() {
global $wp_rewrite;
//修改page的固定链接结构
$wp_rewrite->page_structure = $wp_rewrite->root . 'post/%pagename%.html';
//修改tag的固定链接结构
$wp_rewrite->extra_permastructs['post_tag']['with_front'] = '';
$wp_rewrite->extra_permastructs['post_tag']['struct'] = 'tag/%post_tag%';
}
add_action( 'init', 'custom_page_rules' );

把上面的这段代码放到function.php里,就能自定义 1)page的固定链接结构,2)tag页的固定链接结构。

修改分类目录页(category)的固定链接也是同样的道理。

严谨的说,Wordpress里并没有提供安全的接口来实现这些修改,上面的这些方法属于不安全的入侵,如果某天Wordpress升级了数据结构,那这些修改方法将会失效,甚至会引起系统错误等,所以,有一定风险。

大家的感觉呢?

炫意HTML5 » 自定义WordPress里标签(tag)和分类目录(category)的固定链接结构的方法

Java基础教程Android基础教程