• I am using WP as a CMS so I am not using the WP packaged archives. Instead, I am using a a custom page template to display posts from a specific category by date so they look like this:

    /——————————————-start example———————————————————-/

    –Category 2 Posts From 2006–

    –January–

    post date, post title, comments
    post date, post title, comments
    repeat…

    –February–

    post date, post title, comments
    post date, post title, comments
    repeat…

    –Repeat..–

    –Category 2 Posts from 2005–

    Repeat same as above…

    /——————————————-end example———————————————————-/

    The pertinent page template code looks like this:

    /——————————————-start code———————————————————-/

    <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>
    ***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.***
    </div><!--end entries from 2006-->
    <div class="entries">
    <h5><a name="05" id="05">Questions From 2005</a></h5>
    ****I then repeat the same queries and definition list for each of the 12 months of 2005****
    </div><!--end entries from 2005-->

    /———————————————-end code————————————————————–/

    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:

    1. How can I dynamically display the month name in the <dt> tags, since it has been queried just above the <dt>?

    2. 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.

    3. Of course I would like to be able to generate the year dynamically as well, but that may be asking too much.

    4. 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

  • The topic ‘PHP Coding Page Template’ is closed to new replies.