How can I get archives for specific category without category_base in the url?

I’m using this code to get archives for a specific category:

function extend_date_archives_add_rewrite_rules($wp_rewrite){
    $rules = array();
    $structures = array(
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
    );
    foreach( $structures as $s ){
        $rules += $wp_rewrite->generate_rewrite_rules($s);
    }
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'extend_date_archives_add_rewrite_rules');

It works fine and I get an URL like:
example.com/category_base/category_name/date/YYYY/MM

Read More

The problem is that I would like to have an URL without the category_base: example.com/category_name/date/YYYY/MM

If I use a plugin to delete de “category_base” (like WP no category base) or simply I use “str_replace” to delete the “category_base”, I get a 404 error page in all pages and single pages.

What can I do?

Thanks.

Added notes:

If I use:

function extend_date_archives_flush_rewrite_rules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action('init', 'extend_date_archives_flush_rewrite_rules');
function extend_date_archives_add_rewrite_rules($wp_rewrite){
    $rules = array();
    $structures = array(
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
    );
    foreach( $structures as $s ){
        $rules += $wp_rewrite->generate_rewrite_rules($s);
    }
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'extend_date_archives_add_rewrite_rules');

example.com/category_name/YYYY works fine, but single and pages go to 404 page. But If I only use:

function extend_date_archives_add_rewrite_rules($wp_rewrite){
        $rules = array();
        $structures = array(
            $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
            $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
            $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
        );
        foreach( $structures as $s ){
            $rules += $wp_rewrite->generate_rewrite_rules($s);
        }
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    }
    add_action('generate_rewrite_rules', 'extend_date_archives_add_rewrite_rules');

Then all single and pages work fine, but example.com/category_name/YYYY goes to 404.
Any idea? I refresh permalinks, and I continue with the problem 🙁

Related posts

Leave a Reply

1 comment

  1. Update: Tried and works ok only without the WP no category base plugin, this is, you have to cope with the category though you can change it freely in Permalink settings.

    For a full URL in single posts, set the category structure in Settings > Permalinks, then in Category Base enter /%category%/%postname%/ (image below).

    Custom Structure must be <code/>/%category%/%postname%/“></p>
<p>The function I teste is:</p>
<pre><code>function extend_date_archives_flush_rewrite_rules(){
   global $wp_rewrite;
   $wp_rewrite->flush_rules();
}
add_action('init', 'extend_date_archives_flush_rewrite_rules');

function extend_date_archives_add_rewrite_rules($wp_rewrite){
$rules = array();
$structures = array(
    $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
    $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
    $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
);
foreach( $structures as $s ){
    $rules += $wp_rewrite->generate_rewrite_rules($s);
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}

add_action('generate_rewrite_rules','extend_date_archives_add_rewrite_rules');
</code></pre>
<p>Then You can access year <code>category/categoryname/2012/02</code>, month  <code>category/categoryname/2012/02</code> and day <code>site.com/category/the-categoryname/2012/02/25</code>.</p>
<p>I think you can try merge the plugin rewrite with this function because two rewrites seems to cause some conflict.</p>
				</div><!-- .comment-content -->

				<div class=