在 WordPress 小部件中显示帖子

Display Posts in WordPress widget

我正在尝试过滤要在 WordPress 的短代码中显示的某些类别。 我正在使用下面的内容,它显示了 2 个帖子,但我希望能够说 "display latest posts with category "Apples" 或类别 "Bananas" 的最新 post 我只是不知道如何执行过滤命令。

add_shortcode( 'latest_posts', 'latest_posts' );
function latest_posts( $atts ) {
ob_start();
$query = new WP_Query( array(
    'post_type' => 'post', 'posts_per_page' =>2,'order' => 'DESC'  ));
if ( $query->have_posts() ) { ?>
<?php while ($query->have_posts() ) : $query->the_post(); ?>

    <div class="news-mini">
      <p class="newsdate"><?php echo the_time(); ?></p>
      <h2 class="newshead"><?php the_title(); ?></h2>
      <a class="more-link" href="<?php the_permalink(); ?>" target="_blank">Read More &gt;&gt;</a>
      <hr>
      </div>
<?php  endwhile;
        wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}

如果您想按类别名称加载一些类别帖子,请像下面这样更新 WP_Query

$query = new WP_Query( array('post_type' => 'post', 
    'posts_per_page' =>2,
    'order' => 'DESC',
    'tax_query' => array(
            'taxonomy' => 'NAME OF YOUR CATEGORY',
            'field'    => 'slug',
            'terms'    => 'category',
        ),
    )));