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

categories - custom permalinks for category archive ( custom base and no parent slug )

programmeradmin2浏览0评论

For the default taxonomy i.e category , I want custom permalinks with

i) base = "post/c"  and 
ii) without any parent slug

For eg: if I have categories

cat-a
   subcat-a1
   subcat-a2
cat-b
   subcat-a2
   subcat-a2

so I want permalinks like

example/post/c/cat-a example/post/c/subcat-a1 example/post/c/subcat-a2 example/post/c/cat-b example/post/c/subcat-b1 example/post/c/subcat-b2

I am using the following code .. I solves the issue (ii) but not issue (i) i.e base 'post/c'

add_filter('term_link', 'ojasya_no_term_parents', 1000, 3);
function ojasya_no_term_parents($url, $term, $taxonomy) {
if($taxonomy == 'category'){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
}
return $url;
}
// Add our custom post cat rewrite rules
add_filter('rewrite_rules_array', 'ojasya_no_category_parents_rewrite_rules');
function ojasya_no_category_parents_rewrite_rules($rules) {
$new_rules = array();
$terms = get_terms( array(
'taxonomy' => 'category',
'post_type' => 'post',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
$new_rules[$term_slug.'/?$'] = 'index.php?category='.$term_slug;
$new_rules[$term_slug.'/page/([0-9]{1,})/?$'] = 'index.php?category='.$term_slug.'&paged=$matches[1]';
$new_rules[$term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category='.$term_slug.'&feed=$matches[1]';
}
}
return $new_rules + $rules;
}

Please help me out on this . Thanks in advance

For the default taxonomy i.e category , I want custom permalinks with

i) base = "post/c"  and 
ii) without any parent slug

For eg: if I have categories

cat-a
   subcat-a1
   subcat-a2
cat-b
   subcat-a2
   subcat-a2

so I want permalinks like

example/post/c/cat-a example/post/c/subcat-a1 example/post/c/subcat-a2 example/post/c/cat-b example/post/c/subcat-b1 example/post/c/subcat-b2

I am using the following code .. I solves the issue (ii) but not issue (i) i.e base 'post/c'

add_filter('term_link', 'ojasya_no_term_parents', 1000, 3);
function ojasya_no_term_parents($url, $term, $taxonomy) {
if($taxonomy == 'category'){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
}
return $url;
}
// Add our custom post cat rewrite rules
add_filter('rewrite_rules_array', 'ojasya_no_category_parents_rewrite_rules');
function ojasya_no_category_parents_rewrite_rules($rules) {
$new_rules = array();
$terms = get_terms( array(
'taxonomy' => 'category',
'post_type' => 'post',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
$new_rules[$term_slug.'/?$'] = 'index.php?category='.$term_slug;
$new_rules[$term_slug.'/page/([0-9]{1,})/?$'] = 'index.php?category='.$term_slug.'&paged=$matches[1]';
$new_rules[$term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category='.$term_slug.'&feed=$matches[1]';
}
}
return $new_rules + $rules;
}

Please help me out on this . Thanks in advance

Share Improve this question edited Sep 10, 2018 at 14:06 terminator asked Sep 8, 2018 at 11:44 terminatorterminator 6151 gold badge6 silver badges30 bronze badges 1
  • What it is I am missing? If you have permalinks set to %post_name% and you have changed the category base to what you desire. What you want should be working without editing the changes you describe? – toni_lehtimaki Commented Sep 16, 2018 at 10:30
Add a comment  | 

2 Answers 2

Reset to default 1

All you have to do to achieve this is to modify the category taxonomy arguments by using register_taxonomy() again at the init hook after the category taxonomy has been registered. This can be done with any taxonomy as in the following snippet.

Code for Taxonomies in General (Code for this use case below):

function mytheme_change_category_args() {

    // Get the taxonomy.
    $category = get_taxonomy( 'category' );

    // change the rewrite slug on the taxonomy itself.
    // Note that if your permalink structure is currently 
    // "/post/%postname%", this should be changed to "c", and 
    // "with_front" should be set to "true".
    $category->rewrite['slug'] = 'post/c'; 

    // do not use the text that comes before the first 
    // rewrite tag (%...%) in the permalink settings, unless the
    // setting is "/post/%postname%" as explained above. 
    $category->rewrite['with_front'] = false; 

    // Keep the category hierarchical, but make the permalinks flat.
    $category->rewrite['hierarchical'] = false;

    // register_taxonomy() is used to create OR modify a taxonomy. 
    // make sure the taxonomy object is cast to an array.
    register_taxonomy( 'category', 'post', (array) $category ); 

}

// Make sure this happens after the original tax is init. Priority 10 is fine.
add_action( 'init', 'mytheme_change_category_args', 10 );

Note:

Go to Settings->Permalinksand hit "Save Changes" to flush the rewrite rules. No need to filter term links, add rewrite rules, or anything else.


To answer your question specifically:

In order to do this with the core WP taxonomies like category and post_tag, we have to keep in mind the Category and Tag permalink settings at Settings->Permalinks Which the above example does not. The best way to do this is actually the simplest way too.

Code for this use case:

Option 1:

function mytheme_change_category_args() {

    $category = get_taxonomy( 'category' );
    $post_tag = get_taxonomy( 'post_tag' );

    $category->rewrite['hierarchical'] = false;
    $category->rewrite['with_front'] = false;
    $post_tag->rewrite['with_front'] = false;

    register_taxonomy( 'category', 'post', (array) $category ); 
    register_taxonomy( 'post_tag', 'post', (array) $post_tag ); 

}

add_action( 'init', 'mytheme_change_category_args', 50 );

All you have to do is set category to non-hierarchical permalinks, and set with_front to false on both category and post_tag. then set your permalinks at Settings->Permalinks as in the image below ;)

Option 2:

function mytheme_change_category_args() {

    $category = get_taxonomy( 'category' );
    $post_tag = get_taxonomy( 'post_tag' );

    $category->rewrite['hierarchical'] = false;
    $category->rewrite['with_front'] = true;
    $post_tag->rewrite['with_front'] = true;

    register_taxonomy( 'category', 'post', (array) $category ); 
    register_taxonomy( 'post_tag', 'post', (array) $post_tag ); 

}

add_action( 'init', 'mytheme_change_category_args', 50 );

In this case, what you have to do is set category to non-hierarchical permalinks (as in Option 1), and set with_front to true on both category and post_tag. Then, set your permalinks at Settings->Permalinks as in the image below, keeping in mind that any plain text tags that you place before the first rewrite tag (%...%) of the POST PERMALINK setting will appear before the cat/tag permalink base. If this is set to /post/%...%/%...%/%...%/, this is the best option, for sure.

if you will add post id in url anywhere then it will easy otherwise your post title must be unique. So get whole post by title and use add_rewrite_rule function.

发布评论

评论列表(0)

  1. 暂无评论