I am using a a custom page template and PHP to display posts from a specific category by date in Wordpress so they look like this:
[B][SIZE=4]Posts From 2006[/SIZE]
[/B]
[B]--January--[/B]
post date, post title, comments
post date, post title, comments
repeat...
[B]--February--[/B]
post date, post title, comments
post date, post title, comments
repeat...
[B]--Repeat..--[/B]
[B][SIZE=4]Posts from 2005[/SIZE][/B]
Repeat same as above...
The pertinent page template code looks like this:
<div class="entries">
<h5><a name="06" id="06">Questions From 2006</a></h5>
<?php query_posts('cat=2&year=2006&monthnum=1'); ?>
<dl>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<dt>January</dt>
<dd><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><span class="comments_number"><?php comments_number('0', '1', '%', ''); ?></span><span class="date"><?php the_date('j','',''); ?></span><?php the_title(); ?></a></dd>
<?php endwhile; endif; ?>
</dl>
<?php query_posts('cat=2&year=2006&monthnum=2'); ?>
<dl>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<dt>February</dt>
<dd><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><span class="comments_number"><?php comments_number('0', '1', '%', ''); ?></span><span class="date"><?php the_date('j','',''); ?></span><?php the_title(); ?></a></dd>
<?php endwhile; endif; ?>
</dl>
[B]***I then repeat this with a new query and definition list for each of the 12 months, and if I don't have any posts for that month I have to manually omit it.***[/B]
</div><!--end entries from 2006-->
<div class="entries">
<h5><a name="05" id="05">Questions From 2005</a></h5>
[B]****I then repeat the same queries and definition list for each of the 12 months of 2005****[/B]
</div><!--end entries from 2005-->
As you can see I am having to manually add a new div for each new year(which isn't a big deal). The big problem is having to add the months manually as they go by, and I know there has to be a way to do this dynamically with PHP.
So here are my questions:
How can I dynamically display the month name in the <dt> tags, since it has been queried just above the <dt>?
How can I wrap the definition list in an PHP IF statement so that IF the query_posts for that specific year and month returns nothing the <dl> for that month is skipped?
more specifically---> Have WP run a query like <?php query_posts('cat=2&year=2006&monthnum=2'); ?> then IF there are posts in the query then output the <dl>, otherwise continue and do nothing.
Of course I would like to be able to generate the year dynamically as well, but that may be asking too much.
Also, is there an easier way of accomplishing all this that I am missing? If so please let me know!
If anyone can help me I would appreciate it greatly.
Thanks