• Hi! So I want to get urls like this:
    1) For categories – mywordpresssite.com/category/%category_id%. Example:
    mywordpresssite.com/category/1

    1) For tags the same thing – mywordpresssite.com/tag/%tag_id%. Example:
    mywordpresssite.com/tag/1

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can try something like this:

    add_action( 'init', 'so6159452_init' );
    function so6159452_init()
    {
        add_rewrite_tag( '%category_id%', '([0-9]+)' );
    }
    
    add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
    function so6159452_post_link( $permalink, $post )
    {
        if ( false !== strpos( $permalink, '%category_id%' ) ) {
            $cats = get_the_category( $post->ID );
            if ( $cats ) {
                usort( $cats, '_usort_terms_by_ID' ); // order by ID
                $category_id = $cats[0]->cat_ID;
            } else {
                // Error: no category assigned to this post
                // Just use a dummy variable
                $category_id = '0';
            }
            $permalink = str_replace( '%category_id%', $category_id, $permalink );
        }
        return $permalink;
    }

    Please note this code is not mine. It has been taken from this stackoverflow answer.
    Giving credit where it is due.
    o if you find this useful, go to the above SO link and upvote the answer

    Hi,
    Try out this plugin https://wordpress.org/plugins/custom-permalinks/. Hope it will help you sure and let me know the feedback.

    Thread Starter smck87

    (@smck87)

    Thanks for answers, but I decided to use built in solution and stopped at default categories permalinks.

    You can follow the previous reply of @shariqkhan2012. It should help to you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to change permalink template for categories and tags to use ids’ is closed to new replies.