Pete Leaning
Pete Leaning

Reputation: 486

Wordpress urls for categories and years

I'm new to wordpress and have a question about urls and archived items.

Basically I have two categories of posts: news-item and media-coverage.

when you go to the 'news item' or 'media coverage' section of the blog - (myblog.com/news-item/) , a menu containing each year a post was made in that category is generated in the left navigation. What I want is for a user to be able to click one of those links and view all posts of specific category and year.

for example the url would lead to

myblog.com/media-coverage/2006/ 

and from there next page would lead to

myblog.com/media-coverage/2006/page/2/

then the template would look something like this:

 query_posts('category_name=media-coverage&m=year'); 
   if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="entry">
     <?php the_content(); ?>
     <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
  </div>
  <?php endwhile; 
   endif; ?>

I can't for the life of me figure out how to make wordpress recoginze the 'year' part of the URL. I can hardcode a year in the 'query_posts' function like

   query_posts('category_name=media-coverage&m=2007'); 

and it does exactly what I want, I just can't make it pull the date out of the url, and I'd rather not write a template for every year/category combination. How can I make wordpress filter links in the way I need it to?

edit: I currently have permalinks set to /%postname%/ ,not sure if that helps.

Upvotes: 1

Views: 1478

Answers (1)

fcingolani
fcingolani

Reputation: 576

Try this:

register_activation_hook( __FILE__, 'flush_rewrite_rules' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );


function so7422487_add_rewrite_rules( $wp_rewrite ) {
    $new_rules = array( 
        '(.+)/(.+)' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&year='.$wp_rewrite->preg_index(2) );

    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_action('generate_rewrite_rules', 'so7422487_add_rewrite_rules');

I've saved it as a plugin at Gist. You could download that file and upload it to your plugins directory.

Please tell me how it went.

Upvotes: 2

Related Questions