【问题标题】:Exclude current post from loop从循环中排除当前帖子
【发布时间】:2015-06-23 18:59:58
【问题描述】:

我想在排除当前帖子的帖子模板中为特定类别添加 Wordpress 循环。

有人建议我使用:

<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>

但我无法让它工作。

我的循环目前看起来像这样。

<div class="video">
    <?php
        $catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
        while($catquery->have_posts()) : $catquery->the_post();
    ?>

    <div>
        <a href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail(); ?>
            <h2><?php the_title(); ?></h2>
        </a>
    </div>

    <?php endwhile; ?>

    <p class="more">M<br>O<br>R<br>E</p>
</div>

【问题讨论】:

    标签: php wordpress while-loop categories


    【解决方案1】:

    试试这个代码。

    $postid = get_the_ID();
        $args=array(
          'post__not_in'=> array($postid),
          'post_type' => 'post',
           'category_name'=>'video',
          'post_status' => 'publish',
          'posts_per_page' => 4
    
        );
    
    
        <div class="video">
            <?php
                $catquery = new WP_Query( $args );
                while($catquery->have_posts()) : $catquery->the_post();
            ?>
    
            <div>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail(); ?>
                    <h2><?php the_title(); ?></h2>
                </a>
            </div>
    
            <?php endwhile; ?>
    
            <p class="more">M<br>O<br>R<br>E</p>
        </div>
    

    【讨论】:

      【解决方案2】:

      使用

      'post__not_in' => array($post->ID)
      

      【讨论】:

      • 如何将它添加到我上面的循环中?
      【解决方案3】:

      这两个代码块对 Wordpress 自定义循环使用了两种不同的技术……第一个修改全局查询,第二个创建一个新的自定义查询。我已经用你的循环模板在下面概述了这两个。

      建议代码示例,全局查询:

      在循环代码中循环遍历全局 $wp_query 对象:

      <div class="video">
      <?php
          global $wp_query;
          $cat_ID = get_the_category($post->ID);
          $cat_ID = $cat_ID[0]->cat_ID;
          $this_post = $post->ID;
          query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
      ?>
      
      <!-- use the global loop here -->
      
      <?php while ( have_posts() ) : the_post(); ?>
      
      
          <div>
              <a href="<?php the_permalink(); ?>">
                  <?php the_post_thumbnail(); ?>
                  <h2><?php the_title(); ?></h2>
              </a>
          </div>
      
      <?php endwhile; ?>
      
      <p class="more">M<br>O<br>R<br>E</p
      </div>
      

      原始代码示例,自定义查询

      遍历自定义查询,添加“post__not_in”:

      <div class="video">
      <?php
          $catquery = new WP_Query(     'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
          while($catquery->have_posts()) : $catquery->the_post();
      ?>
      
          <div>
              <a href="<?php the_permalink(); ?>">
                  <?php the_post_thumbnail(); ?>
                  <h2><?php the_title(); ?></h2>
              </a>
          </div>
      
          <?php endwhile; ?>
      
          <p class="more">M<br>O<br>R<br>E</p>
      </div>
      

      对不起,如果我最初的答案不清楚,我最初以为你是在组合两个代码块。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 2013-01-21
        • 2016-12-20
        • 1970-01-01
        相关资源
        最近更新 更多