In my archive template, I’m trying to display either the category or the month and year that the user selected. I’ve figured out how to display the category name using the following code, but obviously this doesn’t work if the user is attempting to view posts by month / year (nothing is displayed):
<div style="border-top:2px solid #93B876;border-bottom:2px solid #93B876;text-align:center;padding:6px;background:#F2F8EF;margin:0 0 10px 0;">You're viewing posts from the <strong><?php single_cat_title(); ?></strong> category</div>
To see exactly what I’m trying to accomplish, take a look at the post heading (the green box above the first post) on my archive page when displayed by category (which is doing exactly what I want):
http://wearethedays.com/category/trips-events/
And now, here it is displayed by date (This is obviously where I’d like it to display the month and year in the post heading as opposed to the category):
http://wearethedays.com/2009/09/
I’m sure I just need a little if/then logic coupled with the correct date function, however my search for this syntax has turned up fruitless. I’d greatly appreciate a push in the right direction.
Thank you!
You can copy the logic right out of archive.php in the default template that comes with WordPress:
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h2 class="pagetitle">Posts Tagged ‘<?php single_tag_title(); ?>’</h2>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('F, Y'); ?></h2>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h2 class="pagetitle">Author Archive</h2>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h2 class="pagetitle">Blog Archives</h2>
<?php } ?>
Thanks Dan. I actually managed to get it working a short while ago, but this is exactly what I needed.