WordPress Code Snippets

A fresh mix of code snippets and tutorials

Post format archives

121

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');
	// POST FORMATS
	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');

	// DATE ARCHIVES
	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');
	endif;
?></h1>

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.

Example

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') : ?>
		<h2>Aside Archives</h2>

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

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

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

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

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

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

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

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

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

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

	<?php // OTHERS
		else : ?>
		<h2>Archives</h2>

	<?php endif; ?>

</header>

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

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

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

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

<?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

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!