I’m trying to create a archive list with only my “normal” post format articles (not link, aside, quote, etc formats).
How would I implement has_post_format( 'standard' )
, or something similar, into the code below?
I haven’t been able to find a query for get_posts
that only requests specific format types.
<?php
// Get the posts
$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
?>
<?php foreach($myposts as $post) : ?>
<?php
// Setup the post variables
setup_postdata($post);
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
?>
<p>
<span class="the_article">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</span>
<span class="the_day">
<?php the_time('j F Y'); ?>
</span>
</p>
<?php endforeach; ?>
My php skills are at the beginner level at best, so any help would be much appreciated.
You can’t actually pass a taxonomy-related argument to(Edit: actually, yes you can. The Codex is just somewhat unclear. Looking at source,get_posts()
.get_posts()
is, at its heart, just a wrapper forWP_Query()
.) You can pass meta keys/values, and post types, but not taxonomies such as post format. So for this line:I would recommend using
WP_Query()
rather thanget_posts()
:Note: yes, that’s a lot of nested arrays. Tax queries can be tricky like that.
The next step is to modify your loop open/close statements. Change these:
…to this:
Your actual loop markup should be able to remain the same, except that you no longer need to call
setup_postdata( $post )
:So, putting it all together:
Post formats are just predefined terms in a taxonomy called
post_format
, so you should be able to use the WP template hierarchy to create post format archives. Just create a file calledtaxonomy-post_format-post-format-standard.php
in the root of your theme and that file will be used to output all your standard posts. You can substitute ‘standard’ with any of the other format names, likeaside
,link
orvideo
, so e.g.taxonomy-post_format-post-format-video.php
. This works for any other taxonomy as well, btw, as long as you stick to this format:taxonomy-{TAXONOMY_NAME}-{TERM_NAME}.php
If you want to show post formats with a custom loop, e.g. in your sidebar or within a page template, then you can use the tax query from @kaiser. Just substitute the taxonomy with
post_format
and the slugs withpost-format-{FORMAT_NAME}
.For two different taxonomies. For a single one, you can leave the
relation
arg out.You can do trick like that:
It is because get_post_format() for standard post format returns false.
http://codex.wordpress.org/Function_Reference/get_post_format