2015-10-29 63 views
0

我試圖創建一個循環,在某些帖子結束時,將拉動相關類別的帖子並排除當前帖子。我覺得我很接近,但仍然沒有工作。這只是因爲某些原因而拉出所有帖子。任何幫助將非常感激。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 --> 

回答

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