1

I want to change the article url as "www..com/post/%post_id%". For example, www..com/post/156/ And now, I did it.

But I don't know how to change the category like this. For example, www.***.com/category/12/.

Generally, the category url is www..com/category/cat1/cat1.1/cat1.1.1/. I want to get www..com/category/cat1.1.1/( just show the last subcategory ).

And now I want to only display the ID of the cat1.1.1, like www.***.com/category/12/ ( the id of cat1.1.1 is 12 )

Who can help me?

  • can you clarify what you mean in the last part about the subcategory? Can you give an example? – Tom J Nowell Dec 1 '19 at 0:54
  • For example, generally, the category url is www.***.com/category/cat1/cat1.1/cat1.1.1/, and now, I want to get this, www.***.com/category/cat1.1.1/. This is my meaning. And now I want to only display the ID of the cat1.1.1, like www.***.com/category/12/ ( the id of cat1.1.1 is 12 ) – Glu Dec 1 '19 at 4:56
1

I find my answer on the internet.

function numeric_category_rewrite_rules( $rules ) {
    $custom_rules = array();
    foreach ( $rules as $regex => $rewrite ) {
        $regex = str_replace( '/(.+?)/', '/([0-9]{1,})/', $regex );
        $rewrite = str_replace( '?category_name=', '?cat=', $rewrite );
        $custom_rules[ $regex ] = $rewrite;
    }
    return $custom_rules;
}
add_filter( 'category_rewrite_rules', 'numeric_category_rewrite_rules' );

function numeric_category_link( $category_link, $term_id ) {
    global $wp_rewrite;
    if ( $wp_rewrite->using_permalinks() ) {
        $permastruct = $wp_rewrite->get_extra_permastruct( 'category' );
        $permastruct = str_replace( '%category%', $term_id, $permastruct );
        $category_link = home_url( user_trailingslashit( $permastruct, 'category' ) );
    }
    return $category_link;
}
add_filter( 'category_link', 'numeric_category_link', 10, 2 );
-1

Go to Admin panel -> Settings -> Permalinks. Change your permalink structure to numerical. Also change your category base to category . This will give you the permalink structure you're asking for, I.E:

www.***.com/category/12/

I'm not sure what you mean by

and just show the last subcategory.

  • For example, generally, the category url is www.***.com/category/cat1/cat1.1/cat1.1.1/, and now, I want to get this, www.***.com/category/cat1.1.1/. This is my meaning. And now I want to only display the ID of the cat1.1.1, like www.***.com/category/12/ ( the id of cat1.1.1 is 12 ) – Glu Dec 1 '19 at 4:57
  • Did you try doing as it says in my answer? If you choose numeric permalink stucture you will not have that hierarchical structure you're referring to. Please try it and mark my answer as solved if it works for you. – Spcaeyob Dec 1 '19 at 11:02
  • Yes, I tried, but it's not what I want – Glu Dec 1 '19 at 12:24

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.