How to create li elements automatically by php?

Recently, I have built a website by wordpress. I have created a sidebar using ul…li elements. I wrote the code manually like the following.

<ul class="accordion">
    <li id="one"><a href="#one">2010 News</a>
        <?php query_posts('order=ASC&cat=2&tag=2010&orderby=date'); ?>
        <ul class="sub-menu">
            <?php while (have_posts()) : the_post(); ?>  
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
        <?php endwhile;?> 
        </ul>
    </li>
    <li id="two"><a href="#two">2011 News</a>
        <?php query_posts('order=ASC&cat=2&tag=2011&orderby=date'); ?>
        <ul class="sub-menu">
            <?php while (have_posts()) : the_post(); ?>  
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
        <?php endwhile;?> 
       </ul>
    </li>
</ul>

I just want the code to create the 2012 News,*2013 News* section automatically by adding the li element when someone has posted a new page in the category whose id is 2 and sign the tag with 2012 or 2013. I can modify the code by myself, but others may not know how to modify it. So I just want to make it easier for others. Can anyone help me? Thanks a lot.

Related posts

Leave a Reply

1 comment

  1. <ul class="accordion">
       <?php for ($year = 2010; $year < date('Y'); $year++) { ?>
        <li id="one"><a href="#<?php echo $year;?>"><?php echo $year;?> News</a>
            <?php query_posts("order=ASC&cat=2&tag=$year&orderby=date"); ?>
            <ul class="sub-menu">
                <?php while (have_posts()) : the_post(); ?>  
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
                <?php endwhile; ?> 
            </ul>
        </li>
        <?php } ?>
    </ul>