• Hey guys wondering if someone can help me out

    I have a client who wants me to list all of the posts in a category and group them by the year they were published.

    Example:

    2011
    – Post title
    – Post title
    2010
    – Post title
    – Post title

    Any ideas how to do this?

    I tried wp_get_archives but it only listed the year and I could not list the posts underneath the year

    Thanks

    Andy

Viewing 3 replies - 1 through 3 (of 3 total)
  • AndyT81,

    Here is a plugin you might be interested in:
    http://blog.favrik.com/2007/10/26/wordpress-plugin-recent-posts-grouped-by-date/

    Thread Starter Andy

    (@andyt81)

    Hey Doc4

    Thanks for that. After deleting out the day and month from the plugin code it does exactly what I want except limit the posts to one category.

    Do you know how to just show posts from a select category? I tried adding ‘category’ => ‘nta’ to the array but no luck.

    Thanks again

    Andy

    Thread Starter Andy

    (@andyt81)

    Anyone have any ideas on how to limit the posts to one category?

    Here is the modified plugin code I am using but it pulls all posts from the site and I only want one particular category:

    <?php
    
    function favrik_recent_posts($args = '') {
    	global $wp_locale, $wpdb;
    
        // params fun
        parse_str($args, $r);
        $defaults = array('category_name' => 'NTA', 'group' => '1', 'limit' => '10', 'before' => '<li>', 'after' => '</li>', 'show_post_count' => false, 'show_post_date' => true, 'date' => 'F jS, Y', 'order_by' => 'post_date DESC');
    	$r = array_merge($defaults, $r);
    	extract($r);
    
        // output
        $output    = '';
        $pre       = '';
        $full_date = '';
        $year      = '';
        $month     = '';
        $day       = '';
    
    	// the query
    	$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'");
    	$join  = apply_filters('getarchives_join', "");
        $qry   = "SELECT ID, post_date, post_title, post_name
                  FROM $wpdb->posts $join
                  $where ORDER BY $order_by LIMIT $limit";
    	$arcresults = $wpdb->get_results($qry);
    	if ($arcresults) {
    		foreach ($arcresults as $arcresult) {
    			if ($arcresult->post_date != '0000-00-00 00:00:00') {
    				$url  = get_permalink($arcresult);
    				if ($group == 0) { // dates at the side of the post link
                        $arc_date = date($date, strtotime($arcresult->post_date));
                        $full_date = '<em class="date">' . $arc_date . '</em> ';
                    }
                    if ($group == 1) { // grouping by year then month-day
                        $y = date('Y', strtotime($arcresult->post_date));
                        if ($year != $y)  {
                            $year = $y;
                            $pre = '<li class="year">' . $year . '</li>';
                        }
                    }
                    $text = strip_tags(apply_filters('the_title', $arcresult->post_title));
    				$output .= get_archives_link($url, $text, $format,
                                                  $pre . $before . $full_date,
                                                 $after);
                    $pre = ''; $full_date = '';
    			}
    		}
        }
        echo $output;
    }
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘List all posts in current category and group by year’ is closed to new replies.