A fresh mix of code snippets and tutorials

Post format archives

The Post Format Archives Widget makes it easy to link to your custom-formatted posts. Here is a snippet for conditionally displaying their respective archives.

So let’s say that you’re displaying links to the archive-views of your various custom-formatted posts (i.e., Post Formats). When visitors follow those links, the Post Format archives will be displayed via your theme’s archive.php template (or index.php, depending on the theme). To customize each of the different Post Format archive-views, add the following snippet in archive.php (or similar):

<h1><?php $var = get_query_var('post_format');
	if ($var == 'post-format-aside') :
		_e('Aside Archives', 'wptao');
	elseif ($var == 'post-format-image') : 
		_e('Image Archives', 'wptao');
	elseif ($var == 'post-format-link') : 
		_e('Link Archives', 'wptao');
	elseif ($var == 'post-format-quote') : 
		_e('Quote Archives', 'wptao');
	elseif ($var == 'post-format-status') : 
		_e('Status Archives', 'wptao');

	elseif (is_day()) :
		printf(__('Daily Archives: %s', 'wptao'), '<span>' . get_the_date() . '</span>');
	elseif (is_month()) :
		printf(__('Monthly Archives: %s', 'wptao'), '<span>' . get_the_date(_x('F Y', 'monthly archives date format', 'wptao')) . '</span>');
	elseif (is_year()) :
		printf(__('Yearly Archives: %s', 'wptao'), '<span>' . get_the_date(_x('Y', 'yearly archives date format', 'wptao')) . '</span>');
	else :
		_e('Archives', 'wptao');

Some notes about this code:

  • It assumes the default WP Post Formats, edit to match your own.
  • It should be included in the Loop, after if (have_posts()) and before while (have_posts()).
  • The code for the Date Archives can be removed if not needed.
  • Edit the markup and customize the PHP to suit your design.


Here is an example of this technique implemented in the archive.php Loop:

<?php if (have_posts()) : ?>

<header class="archive-header">

	<?php // POST FORMATS
	$var = get_query_var('post_format');
	if ($var == 'post-format-aside') : ?>
		<h3>Aside Archives</h3>

	<?php elseif ($var == 'post-format-image') : ?>
		<h3>Image Archives</h3>

	<?php elseif ($var == 'post-format-link') : ?>
		<h3>Link Archives</h3>

	<?php elseif ($var == 'post-format-quote') : ?>
		<h3>Quote Archives</h3>

	<?php elseif ($var == 'post-format-status') : ?>
		<h3>Status Archives</h3>

		elseif (is_day()) : ?>
		<h3>Daily archives: <?php the_time('l, F j, Y'); ?></h3>

	<?php elseif (is_month()) : ?>
		<h3>Monthly archives: <?php the_time('F Y'); ?></h3>

	<?php elseif (is_year()) : ?>
		<h3>Yearly archives: <?php the_time('Y'); ?></h3>

	<?php // CAT, TAG, AUTHOR
		elseif (is_category()) : ?>
		<h3>Category: <?php single_cat_title(); ?></h3>

	<?php elseif (is_tag()) : ?>
		<h3>Tag: <?php single_tag_title(); ?></h3>

	<?php elseif (is_author()) : ?>
		<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
		<h3>Posts by: <a href="<?php $author = get_query_var('author'); echo get_author_posts_url($author); ?>"><?php echo $curauth->display_name; ?></a></h3>

	<?php // OTHERS
		else : ?>

	<?php endif; ?>


<?php while (have_posts()) : the_post(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
		<h1><a href="<?php the_permalink(); ?>" title="View full post"><?php the_title(); ?></a></h1>
		<?php the_content(); ?>

<?php endwhile; ?>
<?php else : ?>

<article id="post-not-found">
		<h1>404 - Not Found</h1>
		<p>The requested resource was not found. Please try again or contact the site admin for help.</p>

<?php endif; ?>

As you can see, this snippet goes beyond Post Format archives and provides custom output for just about every type of WordPress archive. You’ve got your Post Formats, Dates, Tags, Categories, and the whole gang. By editing the markup to suit your needs, this Loop template facilitates granular control over your various archive-views.

Cheers people.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress