• Resolved Michiel

    (@michiel)


    With help of this support forum I managed to group posts in my sidebar from the category “photoalbum”. I would like to however group them by year, so that e.g. the albums from 2004 are under 2004, 2003 under 2003. I can of course copy the code below for each year but I figured out there might be a smarter way of doing that… I am however not a php guru and I would not know how this would go… can somebody please enlighten me:-) Many thanks in advance!

    The code:

    <li><h2><?php _e('2005'); ?></h2>
    <ul>
    <?php $temp_query = $wp_query; ?>
    <?php query_posts('category_name=photoalbum&year=2005&orderby=date&showposts=-1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?><br></a>
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>
    </ul>
    </li>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try something like this:

    <?php
    for ($year = 2005; $year > 2000; $year--) {
    <li><h2><?php _e($year); ?></h2>
    <ul>
    <?php $temp_query = $wp_query;
    query_posts("category_name=photoalbum&year=$year&orderby=date&showposts=-1");
    while (have_posts()) : the_post(); ?>
    <id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?><br></a>
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>
    </ul>
    </li>
    <?php } // end for loop

    Totally untested.

    Thread Starter Michiel

    (@michiel)

    Hi! Thanks for this! I thought that “the loop” was not possible inside the sidebar.php, will try a bit further still with the direction you gave:-)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display category posts sorted by year in sidebar, need help with code’ is closed to new replies.