提取相关类别

时间:2015-10-29 21:15:50

标签: php wordpress

我正在尝试创建一个循环,该循环会在某些帖子的末尾显示,这些帖子会从相关类别中提取帖子并排除当前帖子。我觉得我很接近,但它仍然不能正常工作。由于某种原因,目前只是拉动所有帖子。任何帮助将不胜感激。

$args = array(
    'posts_per_page' => 8,
    //'paged' => get_query_var('paged'), 
        'post__not_in' => array($post->ID),
    'category__in' => $cat_ID   
);

// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post(); ?>

<div style="max-width: 1350px; margin: auto;">
<div id="post-container02">
  <div class="post-image"><a href="<?php the_permalink();?>"> <?php the_post_thumbnail(); ?></a></div>
  <div class="post-feed-title"><a href="<?php the_permalink();?>"> <?php the_title(); ?></a></div>
  <div class="read-more-button"><a href="<?php the_permalink(); ?>">Read More</a></div>
</div><!-- post-container01 -->

1 个答案:

答案 0 :(得分:0)

您可能想在主后循环中尝试此方法。它将找到相关类别并避免当前类别。

$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post);
/*whatever you want to output*/
}
wp_reset_postdata();

https://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same-category