自定义分类和重写URL

时间:2015-08-28 作者:Chimenk

我正在修复一些自定义模板。有这样一种自定义分类法

add_action( \'init\', \'custom_taxonomies\', 0 );
function custom_taxonomies() {
register_taxonomy(\'news_category\', \'stfp_news\', array(
    \'hierarchical\' => true,
    \'show_admin_column\' => true,
    \'label\' => __(\'Categories\'),
    \'show_ui\' => true,
    \'query_var\' => false,
    \'rewrite\' => array(
        \'slug\' => \'about/news/category\'),
    \'singular_label\' => __(\'Category\')) );
register_taxonomy(\'news_tag\', \'stfp_news\', array(
    \'hierarchical\' => true,
    \'show_admin_column\' => false,
    \'label\' => __(\'Tags\'),
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array(\'slug\' => \'about/news/tag\'),
    \'singular_label\' => __(\'Tag\')) );
}
然后在news\\u类别后重写,这样他们的格式

/about/news/press
/about/news/event
这是我一直在写的重写规则

add_rewrite_rule(\'^news/press/page/([0-9]+)/?$\', \'index.php?news_category=press&paged=$matches[1]\', \'top\');
add_rewrite_rule(\'^news/notes/page/([0-9]+)/?$\', \'index.php?   news_category=notes&paged=$matches[1]\', \'top\');
add_rewrite_rule(\'^news/events/page/([0-9]+)/?$\', \'index.php?news_category=events&paged=$matches[1]\', \'top\');
如果我试图删除“category”一词,url将无法工作。似乎规则需要3个url段,如/关于/新闻/类别或任何单词,只要有3个段。有什么想法吗?之前谢谢你。

1 个回复
SO网友:Toby Osborne

你不是想达到重写的目的吗?e、 g。

如果您有一个新闻类别作为press,另一个类别作为event,那么只需更改

register_taxonomy(\'news_category\', \'stfp_news\', array(
\'hierarchical\' => true,
\'show_admin_column\' => true,
\'label\' => __(\'Categories\'),
\'show_ui\' => true,
\'query_var\' => false,
\'rewrite\' => array(
    \'slug\' => \'about/news/category\'),
\'singular_label\' => __(\'Category\')) );

register_taxonomy(\'news_category\', \'stfp_news\', array(
\'hierarchical\' => true,
\'show_admin_column\' => true,
\'label\' => __(\'Categories\'),
\'show_ui\' => true,
\'query_var\' => false,
\'rewrite\' => array(
    \'slug\' => \'about/news\'), // this line changed
\'singular_label\' => __(\'Category\')) );
则表示url为:http://yourdoimain/about/news/events 将仅显示属于events news\\u类别的帖子。

还是我遗漏了什么?

相关推荐