最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Custom Taxonomy and Rewrite URL

programmeradmin0浏览0评论

I'm fixing some custom templates. There is this custom taxonomies

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')) );
}

And rewrite it after the news_category so they will format like

/about/news/press
/about/news/event

This is the rewrite rule i've been writing

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');

If i try to remove word 'category', the url wont work. It seems the rule need 3 url segment like /about/news/category or any words as long as it have 3 segment. Any thoughts? Thanks before.

I'm fixing some custom templates. There is this custom taxonomies

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')) );
}

And rewrite it after the news_category so they will format like

/about/news/press
/about/news/event

This is the rewrite rule i've been writing

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');

If i try to remove word 'category', the url wont work. It seems the rule need 3 url segment like /about/news/category or any words as long as it have 3 segment. Any thoughts? Thanks before.

Share Improve this question edited Aug 28, 2015 at 7:21 Chimenk asked Aug 28, 2015 at 7:03 ChimenkChimenk 113 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Isn't what you are trying to achieve the point of the rewrite slug? e.g.

if you have a news category as press and another as event then simply changing

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')) );

to

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')) );

would then mean that a url of: http://yourdoimain/about/news/events would only show posts that belong to the events news_category.

or am I missing something?

发布评论

评论列表(0)

  1. 暂无评论