自定义WordPress标签(tag)和分类目录(category)的固定链接结构的方法
对于Wordpress比较熟悉的朋友应该知道,如果想修改文章(post)的固定链接结构(permalinks),可以到Wordpress后台管理界面进行修改,比如说,如果你想让页面地址都有一个“tag”的前缀,形如:
你可以在后台的“固定链接”设置界面将固定链接结构设置为“/tag/%postname%”就行了。
但是,你会发现,只有文章页(post)的链接结构上这样改的,而一般页面(page)的链接结构没有变化,而且,标签(tag)的链接结构也变成了”/tag/%tag%”样子。
现在的问题是,如果你想自定义标签(tag)的固定链接,让它不随文章页(post)的链接结构的变化而变化,让标签页(tag)具有更个性的链接结构,怎么办呢?
或者,我还想自定义分类目录页(category)的固定链接结构,有什么办法?
我们是应该从$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