3

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.com/post/c/cat-a example.com/post/c/subcat-a1 example.com/post/c/subcat-a2 example.com/post/c/cat-b example.com/post/c/subcat-b1 example.com/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

  • 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 Sep 16 '18 at 10:30
0

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

Cat Tag Permalinks

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.

Permalink Settings Screen Shot

  • @terminator I just tested this to be sure. This is what you want right here (see second example). – noahmason Sep 16 '18 at 21:22
-1

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.

  • It's not really very helpful to say "add_rewrite rule" - what rewrite rule would you propose. In my limited experience, this question, (which is becoming popular) just can't be done. Though I would be excited to see someone's successful answer. – Tedinoz Sep 15 '18 at 9:51
  • i already told add_rewrite rule. With help of this you can make any type of url in wordpress. – David Corp Sep 15 '18 at 10:55
  • I look forward to the forthcoming update to your answer where you demonstrate the detailed rewrite rule that you would recommend to enable the questioner to successfully achieve their goal. – Tedinoz Sep 15 '18 at 11:03
  • here is docs: codex.wordpress.org/Rewrite_API/add_rewrite_rule . I already wasted 1 week in wp custom links. Only it works well and it is also official. – David Corp Sep 15 '18 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.