I want to display all posts of a post format (let's say: aside). In WordPress to display all posts of a certain category, just need to use this URL: mysite.com/category/mycategory. Is there a simil开发者_如何学Goar way to display all posts of a post format? Or any other way is also fine for me.
You could use tax_query parameters to get the posts by post_format. For example:
$args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' )
)
)
);
Then you can iterate over the results with get_posts() (or use WP_Query() and a standard a Loop):
$asides = get_posts( $args );
if ( count($asides) ) {
foreach ( $asides as $aside ) {
// do something here...
}
}
// this will get all 'quote' post format
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-quote' )
)
)
);
$query = new WP_query($args);
while($query->have_posts()) : $query->the_post();
// do whatever you want with it
endwhile;
WordPress creates archive pages for each post format automatically.
Use the get_post_format_link() method to generate the link to the WordPress archive for each post format (although this method does not work with the "standard" post format).
See The Loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
That code above list all of the post on your wordpress site, so to show all post from a category you need the use the if ( in_category('CategoryNumberHere ')
function to only fetch post from a category. You must also know the number of the category in your WordPress installation.
Take a look at the linked page above, it has a full tutorial and layout of how to do such things. The code specific to categories is the second section down.
精彩评论