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

customization - How to change the category url jusy show category%category_id% in wordpress?

programmeradmin2浏览0评论

I want to change the article url as

www.***/post/%post_id%

For example,

www.***/post/156/

And now, I did it.

But I don't know how to change the category like this. For example,

www.***/category/12/

Generally, the category url is:

www.***/category/cat1/cat1.1/cat1.1.1/

I want to get:

www.***/category/cat1.1.1/

And now I want to only display the ID of the cat1.1.1, like:

www.***/category/12/ ( the id of cat1.1.1 is 12 )

I want to change the article url as

www.***/post/%post_id%

For example,

www.***/post/156/

And now, I did it.

But I don't know how to change the category like this. For example,

www.***/category/12/

Generally, the category url is:

www.***/category/cat1/cat1.1/cat1.1.1/

I want to get:

www.***/category/cat1.1.1/

And now I want to only display the ID of the cat1.1.1, like:

www.***/category/12/ ( the id of cat1.1.1 is 12 )
Share Improve this question edited Mar 25, 2020 at 20:28 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Nov 30, 2019 at 18:13 GluGlu 214 bronze badges 2
  • can you clarify what you mean in the last part about the subcategory? Can you give an example? – Tom J Nowell Commented Dec 1, 2019 at 0:54
  • For example, generally, the category url is www.***/category/cat1/cat1.1/cat1.1.1/, and now, I want to get this, www.***/category/cat1.1.1/. This is my meaning. And now I want to only display the ID of the cat1.1.1, like www.***/category/12/ ( the id of cat1.1.1 is 12 ) – Glu Commented Dec 1, 2019 at 4:56
Add a comment  | 

2 Answers 2

Reset to default 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 );

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.***/category/12/

I'm not sure what you mean by

and just show the last subcategory.

发布评论

评论列表(0)

  1. 暂无评论