Site logo

Archived topic

How to show a category in the URL of a blog post

3 replies · Started by Data on January 12, 2022

Viewing posts 1–4 of 4

I created a blog post under the category "Technology" and select the parent category.

The current URL is domain.com/how-to-change-my-username

I want to change to domain.com/technology/how-to-change-my-username

I don't want to change this from the permalink section for the overall website. but only for specific blog posts.

Can we do this through the blog post right panel?

Hi there,

To clarify: Will this be just for a specific single post or will this be applied to a specific post type or a specific category?

Let us know. :D

yes for specific category

You can try this PHP snippet:

function technology_cat_permalink_post( $permalink, $post, $leavename ) {
    // Get the categories for the post
    $category = get_the_category($post->ID); 
    if (  !empty($category) && $category[0]->cat_name == "Technology" ) {
        $permalink = trailingslashit( home_url('/'.$category[0]->slug.'/'. $post->post_name . '/' ) );
    }
    return $permalink;
}
add_filter( 'post_link', 'technology_cat_permalink_post', 10, 3 );
This archived topic is closed to new replies.