Php 根据文章类别在侧栏中显示最近文章标题的列表

Php 根据文章类别在侧栏中显示最近文章标题的列表,php,wordpress,post,sidebar,Php,Wordpress,Post,Sidebar,我试图根据文章所属的类别在侧边栏中显示最新的文章标题。这段代码对于特定的页面模板非常有用,但是如果我把它放在single.php文件中,我只能从一个类别中提取文章标题 有没有一种方法可以根据文章的类别显示文章标题 <!-- BEGIN SIDEBAR --> <div class="col-md-4 column blogsidebar"> <aside id="recent-posts-4" class="widget widget_recent_entries

我试图根据文章所属的类别在侧边栏中显示最新的文章标题。这段代码对于特定的页面模板非常有用,但是如果我把它放在single.php文件中,我只能从一个类别中提取文章标题

有没有一种方法可以根据文章的类别显示文章标题

<!-- BEGIN SIDEBAR -->

<div class="col-md-4 column blogsidebar">
<aside id="recent-posts-4" class="widget widget_recent_entries">        
<h1 class="widget-title">Recent Articles</h1><hr>   
<?php $my_query = new WP_Query('category_name=Blog&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); echo '<br>'; ?>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a><br>
<?php echo word_count(get_the_excerpt(), '12'); ?>...<br>
<?php endwhile; ?><p></p>
</div>

<!-- END SIDEBAR -->   




首先获取可见帖子的类别,然后使用该类别进行查询

$post_cat_ids = wp_get_object_terms( get_the_ID(), 'category', array('fields' => 'ids'));
那么根据你的提问,

<?php 
    $my_query = new WP_Query( array( 
        'category__in' => $post_cat_ids, 
        'showposts' => 10 
    )); 
?>

工作起来很有魅力!非常感谢,你是一个非常聪明的人!