2013-12-18 20 views
1

我试图根据帖子所在类别显示最新的帖子标题。此代码适用于特定页面模板,但如果我把它放在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 --> 

回答

1

首先获取可见帖子的类别,然后用它查询。

$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 
    )); 
?> 
+0

工作就像一个魅力!非常感谢,你是一个非常聪明的人! – pwiz

相关问题