Support » Fixing WordPress » display blog posts sorted by category and year

  • Hello,
    What I am trying to accomplish is this,

    Category one
    2014
    2013
    2012
    Category two
    2014
    2013
    Category three
    2013
    2012
    2011

    I cant seem to find a way to list them automatically this way without hardcoding, the years will be automatic depending on the dates of the posts. The category name will also be a link to display all years in that category

    What I have now is

    <?php
    wp_list_categories( $args )

    wp_get_archives(‘type=yearly’)
    ?>

    <?php $args = array(
    ‘show_option_all’ => ”,
    ‘orderby’ => ‘name’,
    ‘order’ => ‘ASC’,
    ‘style’ => ‘list’,
    ‘show_count’ => 0,
    ‘hide_empty’ => 1,
    ‘use_desc_for_title’ => 1,
    ‘child_of’ => 0,
    ‘feed’ => ”,
    ‘feed_type’ => ”,
    ‘feed_image’ => ”,
    ‘exclude’ => ”,
    ‘exclude_tree’ => ”,
    ‘include’ => ”,
    ‘hierarchical’ => 1,
    ‘title_li’ => __( ” ),
    ‘show_option_none’ => __( ‘No categories’ ),
    ‘number’ => null,
    ‘echo’ => 1,
    ‘depth’ => 0,
    ‘current_category’ => 0,
    ‘pad_counts’ => 0,
    ‘taxonomy’ => ‘category’,
    ‘walker’ => null
    ); ?>

Viewing 1 replies (of 1 total)
  • I think the code below is close to what you want. I believe that you will need a custom archive.php to show only the current category when you click on a year to show the archive for that year.

    Change the add_query_arg() call to add your own custom argument for the category, and then check for that argument in the custom archive.php.

    $args = array(
       'echo' => 0,
       'hierarchical' => false,
       'hide_empty' => true,
    );
    $terms = get_terms('category');
    // print_r('<pre>TERMS:');print_r($terms);print_r('</pre>');
    if ( !empty( $terms ) && !is_wp_error( $terms ) ){
       foreach ( $terms as $term ) {
          if ( $term->count > 0 ) { ?>
             <div class="term_title">
                <?php // print_r("ID: $term->term_id Count: $term->count<br />"); ?>
                <a href="<?php echo get_term_link( $term ); ?>" title="<?php echo "View all posts filed under $term->name"; ?>"><?php echo $term->name; ?></a>
                </div>
             <div class="term_years">
                <?php mam_list_term_years($term->term_id); ?>
             </div>
          <?php }
       }
    }
    function mam_list_term_years($term_id) {
       global $post;
    
       $current_year = '';
       $my_posts = new WP_query("category__in=$term_id&caller_get_posts=1");
       // print_r('<pre>POSTS:');print_r($my_posts);print_r('</pre>');
       if ( $my_posts->have_posts() ) {
          while ( $my_posts->have_posts() ) {
             $my_posts->the_post();
             // print_r('<pre>POST:');print_r($post);print_r('</pre>');
             $post_year = substr($post->post_date, 0, 4);
             // echo "CAT: $term_id, POST DATE: $post->post_date, POST YEAR:$post_year<br />";
             if ( $post_year != $current_year ) {
                $current_year = $post_year;
                $year_link = get_year_link( $current_year );
                $year_link = add_query_arg( 'category__in', $term_id, $year_link ); ?>
                <div class="term_year">
                   <a href="<?php echo $year_link; ?>"><?php echo $current_year; ?></a>
                </div>
             <?php }
          }
       }
    
    }
Viewing 1 replies (of 1 total)
  • The topic ‘display blog posts sorted by category and year’ is closed to new replies.