2

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

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 :(

3
  • If you use the plugin alone the categories, singles, pages etc goes 404? If so there is something wrong in .htaccess or somewhere else. About URLs, iif a page and category has the same URL the category will display, but you can't reach the page anymore, (there's no any system notice on creating content with same URLs so far :p )
    – Diana
    Feb 22, 2013 at 22:33
  • Thanks for your answer, but I would like to get this URL: example.com/category_name/date/YYYY/MM
    – Mike
    Feb 22, 2013 at 23:27
  • have a look at this plugin wordpress.org/extend/plugins/wp-no-category-base Feb 23, 2013 at 15:48

1 Answer 1

0

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%/</code>

The function I teste is:

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');

Then You can access year category/categoryname/2012/02, month category/categoryname/2012/02 and day site.com/category/the-categoryname/2012/02/25.

I think you can try merge the plugin rewrite with this function because two rewrites seems to cause some conflict.

4
  • But have you tried it with or without my function?
    – Mike
    Feb 23, 2013 at 3:18
  • With the function, I think there is no way to do without it.
    – Diana
    Feb 23, 2013 at 3:39
  • I continue with problems. Read my last added notes.
    – Mike
    Feb 23, 2013 at 15:16
  • Hi, sorry I overlooked the single access :( I'm not sure but two rewrite rules at same time (plugin and function) seems to cause a conflict. Is quite a hack to remove the category anyway, there's never been an intend from WP devs in removing it as far as I know :(
    – Diana
    Feb 23, 2013 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.