[英]How to display posts by category in a static page on WordPress
do you know which code should I use to display posts by category in a static page?您知道我应该使用哪个代码在静态页面中按类别显示帖子吗?
<div>
// This is a static page
// Here I want to display posts from specific category ex. category named hello
</div>
The Complete Implementation for fetching the post from a Specific Category and Display the post in a pages layout if you have more post.从特定类别获取帖子的完整实现,如果您有更多帖子,则在页面布局中显示帖子。
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 10,
'category_name' => ‘your category’,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();?>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<h5><?php the_time('F j, Y'); ?></h5>
<div>
<?php if( has_excerpt() ): the_excerpt(); else: the_content(); endif; ?>
</div>
<?php }?>
//Page navigation
<div id="pagenav" class="navigation clearfix">
<div class="nav-prev alignleft"><?php next_posts_link( '<i class="fa fa-angle-double-left" aria-hidden="true"></i> Older Entries', $the_query->max_num_pages ); ?></div>
<div class="nav-prev alignright"><?php previous_posts_link( 'Newer Entries <i class="fa fa-angle-double-right" aria-hidden="true"></i>' ); ?></div>
</div><!-- #pagenav -->
<?php echo '</div>';
} else {
// no posts found
echo '<h1>No Posts Found</h1>';
}
// Restore original Post Data
wp_reset_postdata();
?>
Use the following:使用以下内容:
[display-posts category="fishing,hiking"]
There are lots of other arguments you can give.您可以提供许多其他论据。 This link will be useful: https://en.support.wordpress.com/display-posts-shortcode/
此链接将很有用: https : //en.support.wordpress.com/display-posts-shortcode/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.