Question

What I'm trying to do is allow the site to show date-based archives for terms, but I'm having no luck at all.

How I want this to work:

website.com/taxonomy/term/ works, and is great.

website.com/taxonomy/term/2012 doesn't... it actually 404s. website.com/taxonomy/term/2012/11 doesn't either.

I need some help creating some rewrite rules that will take the url and modify the query to add year={year}&monthnum={$month} (or however that magic works), and also redirect back to the taxonomy-taxonomy.php template, because that seems to break.

Please ask, I'll help clarify as best I can :)

Taxonomy Registration (copied from the project):

register_taxonomy ( 'age-limit', array('event'), array(
        'hierarchical' => false,
        'labels' => array (
            'name' => _x( 'Age limit', 'taxonomy general name' ),
            'singular_name' => _x( 'Age limit', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Age limits' ),
            'all_items' => __( 'All Age limits' ),
            'edit_item' => __( 'Edit Age limit' ), 
            'update_item' => __( 'Update Age limit' ),
            'add_new_item' => __( 'Add New Age limit' ),
            'new_item_name' => __( 'New Age limit' ),
            'menu_name' => __( 'Age limits' ),
        ),
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'age-limit'),
  ));
Était-ce utile?

La solution

About rewriting the rules... you can use these lines of code (tested and working). Don't know about the redirection thing you mention though.

add_filter('rewrite_rules_array', '_rewrite_rules_array');
function _rewrite_rules_array($rewrite_rules) {

  $taxonomy = "age-limit";
  $taxonomy_slug = "age-limit";


  $month_rule = array($taxonomy_slug .'/(.+?)/([0-9]{4})/([0-9]{1,2})/?$'   => 'index.php?' . $taxonomy . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]');
  $year_rule = array($taxonomy_slug . '/(.+?)/([0-9]{4})/?$' => 'index.php?' . $taxonomy . '=$matches[1]&year=$matches[2]');

  $rewrite_rules = $year_rule + $month_rule + $rewrite_rules;

  return $rewrite_rules;
}

This WordPress plugin helped me understand rewrite rules a little better: Rewrite Rules Inspector

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top