WordPress URL Trick: Categories and Posts Under One Path

  • Post category:Development
  • Reading time:5 mins read
  • Post comments:0 Comments
You are currently viewing WordPress URL Trick: Categories and Posts Under One Path

By default, WordPress offers several URL schemes for blog posts, as well as a separate URL for categories. But what if you wanted to store both individual posts and category lists under a single scheme? With a few simple tricks, this can easily be achieved.

The Goal

Let’s suppose you wanted to create a permalink structure where all your category lists and your posts are all sitting under a “/docs” location. As an example, suppose you had a “Travel” category, an “Italy” subcategory under it, and posts in both the Travel category and the Italy subcategory. Your goal is to have URLs such as:

  • /docs
    Root location for all categories and posts
  • /docs/travel
    All posts in the travel category
  • /docs/travel/packing-hacks
    A post under the Travel category
  • /docs/travel/italy/
    All posts in the Italy subcategory
  • /docs/travel/italy/southern-italy-2025
    A post under the Italy subcategory

What WordPress Provides for These URLs

In the WordPress admin console, under Settings > Permalinks, you can configure various permalink structures for your posts. These include the following pre-defined options:

  • Plainhttps://example.com/?p=123
  • Day and namehttps://example.com/2025/07/29/sample-post/
  • Month and namehttps://example.com/2025/07/sample-post/
  • Numerichttps://example.com/archives/123
  • Post namehttps://example.com/sample-post/

In addition, there is the option to set a Custom Structure, where you can configure any URL structure with placeholders for the year, month number, day, hour, minute, second, post id, post name, category and author. By default, the default structure is set to %postname%, making it the same as the “Post name” option above.

On the same settings page you can also set the Category Base. If you do not set it, WordPress will use “category” and the URL for your Travel posts would be /category/travel.

Customizing the Settings

From the Settings > Permalinks page we can now make some small adjustments as the first step to achieve our goal:

  • Permalink Structure: Select Custom Structure and set to /docs/%category%/%postname%
  • Category Base: Set to docs/.

The value set for the permalink structure will ensure all posts appear under “docs” and then under their corresponding category. If a document is under a subcategory or even deeper nested subcategories, WordPress will automatically handle it, replacing the %category% with the appropriate path to the subcategory where the post resides.

WordPress doesn’t normally expect the category base to overlap with the post path. Including the /. at the end of the category base allows you to bypass this restriction, and when WordPress builds the URLs for routing, it will normalize the path and remove this from the category base while assembling the full route.

Once you set this, you may be tempted to start testing out your shiny new permalink scheme. Before you do, you’ll want to read the next section so you’re not shocked when something doesn’t work quite right.

Fixing the 404 Errors

At first glance, the settings we made above should allow you to have the same name for the posts and categories. If you test the categories and subcategories, they’ll work quite well and you’ll be impressed with yourself. Unfortunately, at this point I’m sure you realize not everything is that simple when customizing WordPress, and it will return 404 errors for the posts! However, this just requires a little customization to address.

While it’s outside the scope of this article, you can fix this with a little code in your child theme if you’re using one. If not, or if you just want a simpler approach, you can use a plugin that allows you to add custom snippets. For this, I’d recommend WPCode, although there are others that will work for this as well. The key is that you need to add the following custom PHP code snippet:

// fix for category base
function permalinkCatBaseFix() {
    global $wp_query;
    if ($wp_query->is_404
        && ($uri = trim($_SERVER['REQUEST_URI'] ?? ''))
        && ($catBase = trim(rtrim(get_option('category_base'), '.'), '/'))
		&& preg_match("#^/$catBase($|/)#", $uri)
    ) {
        $childCat = get_category_by_slug($wp_query->query_vars['name']);
        if (is_object($childCat)) {
            $wp_query->query([
                'cat'  => $childCat->term_id,
                'paged'=> $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
            ]);
            status_header(200);
        }
    }
}

// fix for pagination
function fixCatPagination($query) {
    if (($query['name'] ?? '') == 'page' && !empty($query['page'])) {
        #$query['paged'] = explode('/', $query['page'])[1];
        $query['paged'] = $query['page'];
        unset($query['name']);
    }
    return $query;
}

// install fixes
add_action('template_redirect', 'permalinkCatBaseFix');
add_filter('request', 'fixCatPagination');

Setting Categories and Subcategories

You’ll want to put together a well-structured set of categories and if necessary, subcategories, to take advantage of your new permalink structure. I recommend you set the category names to the page titles you want shown when the category post lists are shown. This will depend on the rendering provided by your theme, but almost all will utilize this name. Some will use the category description as subtext under the title. The slug for each category will then become the value used by WordPress under the /docs/ path.

For sub-categories, be sure to set each to be under its parent. With the structured path we now have, I prefer to limit the slug for a subcategory to its context within the parent category, rather than repeating it. For example, using “italy” as the slug so that the URL is /docs/travel/italy allows the structured format of the URL to provide the necessary information to both the user and for SEO purposes, instead of a more repetitive URL like /docs/travel/italy-travel. Of course, you’ll need to determine what works for your needs.

Custom Pages

A nice benefit of this approach is that it allows you to create pages for some portions of this scheme. For example, you can create a “Documents” page with the slug set to docs, which you can then customize. With this approach you can ensure that all paths within the URL scheme can be visited. You can take a path to a post, and should be able to work your way up the URL to each parent directory and still have a route to the appropriate content. This will simulate a directory structure, when in reality WordPress will automatically route to the posts, categories and pages as needed.

Leave a Reply