dongshou1991 2015-10-29 21:15
浏览 30

Wordpress循环拉动相关类别的帖子

I'm trying to create a loop that goes at the end of certain posts that will pull posts from related categories and exclude the current post. I feel like I'm close but it's still not quite working. It's just pulling all posts currently for some reason. Any help would be much appreciated.

$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条回答 默认 最新

  • duancai9010 2015-10-29 22:05
    关注

    You may want to try this method within your main post loop. It will find the related categories and avoid the current category.

    $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

    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元