Wordpress,相关帖子,获得帖子的一个类别

时间:2015-11-20 14:25:58

标签: php wordpress

我正在使用此代码显示相关帖子:

$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
?><h2> More from this category:</h2> <ul class="related-posts"> <?php
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?> 
        <li class="pov">
            <div class="ex-s1">
            <a href="<?php the_permalink();?>">
    <?php the_post_thumbnail(array(356, 220)); ?>
    </a> 
                </div>
            <div class="ex-n1">
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </div>
            <div class="ex-t1">
           <?php echo get_excerpt(250);?>
              </div>
        </li>

<?php }
wp_reset_postdata(); ?>
  </ul>

它有点问题。让我们说我们有post1和post2。它们属于category1和category2。如何在显示当前查看的帖子1的相关帖子的单个页面上仅显示category1中的帖子而不是cat2中的帖子?

THX!

1 个答案:

答案 0 :(得分:1)

只需获取wp_get_post_categories()函数

返回的第一个项目
$cat = wp_get_post_categories($post->ID);
$related = get_posts( array( 'category__in' => $cat[0], 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );