WordPress remove Category from the URL

Learn how to remove category from your Wordpress URL for a cleaner look with an example.

Removing Category from the URL with WordPress

If you’re using WordPress as your CMS, you may want to remove “category” from your post’s URL. This is especially helpful if you’re using categories to organize your content, as it will make the URLs cleaner and more SEO friendly.

In order to remove “category” from the URL, you’ll need to make a few edits to your theme’s functions.php file. First, open the file and add the following code:


function remove_category_base( $link ) {
    $category_base = get_option('category_base') ? get_option('category_base') : 'category';
    $category_base .= '/';

    // Remove initial slash, if there is one (we remove the slash at the end as well)
    if( substr($link, 0, 1) == '/' ) {
        $link = substr($link, 1);
    }

    // Remove category base and the slash at the end
    $link = str_replace($category_base, '', $link);

    return $link;
}
add_filter( 'category_link', 'remove_category_base' );

Once you’ve added the code, it’s important to clear your site’s cache. This will ensure that the changes you’ve made are applied to your site.

You can also change the “category” base by going to the “Settings” tab in the WordPress admin panel. Here, you’ll find an option to change the “Category Base”. Simply type in the new base you’d like to use and click “Save Changes”.

Once you’ve made the changes, all category URLs on your site will no longer have the “category” base. This will make your URLs look much cleaner and more SEO friendly.

Answers (0)