繁体   English   中英

Wordpress循环,可提取具有相关类别的帖子

[英]Wordpress loop that pulls posts with related categories

我正在尝试创建一个循环,该循环在某些帖子的末尾进行,这将从相关类别中拉出帖子,并排除当前帖子。 我觉得我已经接近了,但仍然无法正常工作。 由于某种原因,它只是取消了当前的所有帖子。 任何帮助将非常感激。

$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 -->

您可能要在主发布循环中尝试此方法。 它将找到相关类别,并避免使用当前类别。

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM