what the blog?

{upgrade in progress..}

How to filter “Standard” Post format in WordPress

Post format

As you know Post Formats is a theme feature introduced with Version 3.1. A Post Format is a piece of meta information that can be used by a theme to customize its presentation of a post. The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. There are new template tag to play with post_format as below

WordPress Codex

However, when you try to implement post_format, usually you’ll try to sort out the posts for each formats. For example, show all images format.

<a href="<?php echo get_post_format_link('image');?>">
Show Image Posts
</a>

The above code do present a link taking you to post_fomart archives page, pretty easy right? So… how about display the standand post? Unfortunately, there is no out of box function to do that. Let’s work it out.

functions.php

Setup theme support in functions.php, I put the post formats in vars, because we need to reused them in some place within the theme. Additionally, I created another function to register query variable to handle new query for standard post format.

/* Register Query Variables*/
add_filter('query_vars', 'my_queryvars' );
function my_queryvars( $qvars )
{
$qvars[] = 'filter';
return $qvars;
}
/* Post formats support */
$my_post_formats = array( 'status','aside','image', 'gallery', 'link', 'video', 'audio' );
foreach ($my_post_formats as $shortname) {
	$my_post_formats_longname[] = 'post-format-'.$shortname;
}
add_theme_support( 'post-formats', $my_post_formats );

The filter links

<a href="<?php echo add_query_arg( 'filter','standard',remove_query_arg('post_format'));?>"><?php echo get_post_format_string('');?></a>
<?php global $my_post_formats; foreach ($my_post_formats as $postformat) { ?>
	| <a href="<?php echo get_post_format_link($postformat);?>"><?php echo get_post_format_string(str_replace('post-format-', '', $postformat) ); ?></a>
<?php } ?>

the loop

Put the following code before the loop. This is a new taxonomy query since WordPress 3.0, it allows you to setup own query by taxonomy terms. And you may notice that I’ve put “NOT IN” operator to filter out standard posts. It is because “standard” post do not have post-format- taxonomy. The first condition to determine the from the $_GET request.

<?php if(get_query_var('filter') == "standard") { 	global $my_post_formats_longname; 	$args = array( 			'tax_query' =--> array(
			array( 'taxonomy' => 'post_format',
				  'field' => 'slug',
				  'terms' => $my_post_formats_longname,
				  'operator' => 'NOT IN'
				  )
			)
	);
	query_posts( $args );
}
?>

That’s it

Btw, I hope there have a better solution in future WordPress release. Thanks for reading!

post_formatwordpress

levin • April 15, 2011


Previous Post

Next Post

Leave a Reply