【问题标题】:Wordpress exclude duplicated $sticky post from loopWordpress 从循环中排除重复的 $sticky 帖子
【发布时间】:2016-12-20 13:48:10
【问题描述】:

由于 Wordpress sticky posts 功能允许在帖子发布面板中检查为 sticky 的帖子放置在帖子首页的顶部。我还打算通过自定义The Loop 默认编码如下:

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php while (have_posts()) : the_post(); ?>

     <?php $c++;
       if( is_home() && !$paged && $c == 1 ) :?>
         <!--First sticky post content -->

    <?php elseif( is_home() && !$paged && $c == 2 ) :?>
        <!--Second sticky post content -->

    <?php elseif( is_home() && !$paged && $c == 3 ) :?>
        <!--Third sticky post content -->

    <?php else:?>
        <!-- Standard post content -->

    <?php endif;?>
    <?php endwhile; ?>
    <!-- End of the main loop -->
      //pagination

    <?php else : ?>
      <?php _e('Sorry, no posts matched your criteria.'); ?>

    <?php endif; ?>

结果是我得到了前三个具有自定义样式的帖子(检查为粘性),同时在 标准帖子 输出中重复,但我未能删除。

我尝试将&lt;?php else : ?&gt; 替换为&lt;?php elseif(!is_sticky()) : ?&gt;,但显示的页面已“分页”或当前页码大于1,帖子数根据每页的发布日期减去粘性帖子。

非常感谢任何帮助制作不重复的粘性帖子。

【问题讨论】:

  • 你有 2 个循环还是只有 1 个?如果可能的话,请分享整个代码......
  • 嘿@Ehab,假设您只有一个循环,请在下面查看我的答案????

标签: php wordpress loops


【解决方案1】:

我建议您阅读 WP_Query 文档:
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

您可以创建新的(或更改现有的)查询并获取没有粘性的帖子。

不显示置顶帖

从查询中排除所有置顶帖子:

$query = new WP_Query( array( 'post__not_in' =&gt; get_option( 'sticky_posts' ) ) );

我建议你连续查询 2 次。

第一个获取置顶帖子,第二个获取所有其他帖子(不包括置顶帖子,如上所述)。

【讨论】:

  • 我熟悉查询参数。这将从整个循环中完全删除粘性帖子,而我只需要它们在第一页 codex.wordpress.org/Sticky_Posts
  • 在这种情况下,我建议您使用两个循环,第一个循环获取置顶帖子,第二个循环获取所有其他帖子(不包括置顶帖子)。
【解决方案2】:

要处理置顶帖

<?php
    $sticky_query = new WP_Query( array( 'post__in' => get_option( 'sticky_posts' ) ) );
    while ($sticky_query->have_posts()) : $sticky_query->the_post();
?>

要处理不粘性的帖子

<?php
    $non_sticky_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) ); 
    while ($non_sticky_query->have_posts()) : $non_sticky_query->the_post();
?> 


PS - is_sticky() 在这里不起作用,因为我猜这段代码在你的主页上运行。 is_sticky() 将要求 post_id 在帖子页面以外的页面上按要求工作。

您可以在当前代码的循环内执行类似is_sticky(get_the_ID()) 的操作。

【讨论】:

    【解决方案3】:

    您可以将主页帖子和其他类似帖子分开:

    <?php if (have_posts()) : ?>
    
    <!-- if home page -->
    <?php if( is_home() && !$paged ) :?>
    
      <!-- First page loop -->
      <?php $post = $posts[0]; $c=0;?>
      <?php while (have_posts()) : the_post(); ?>
    
         <?php $c++;
         if( $c == 1 ) { ?>
         <!--First sticky post content -->
         <?php } ?>
    
         <?php if( $c == 2 ) { ?>
         <!--Second sticky post content -->
         <?php } ?>
    
         <?php if( $c == 3 ) { ?>
         <!--Third sticky post content -->
         <?php } ?>
    
      <?php endwhile; ?>
      <!-- End of first page loop -->
    
    <!-- else if not home page -->
    <?php else:?>
    
      <!-- exclude stuicky posts, then run the standard loop -->
      <?php $query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
    
      <?php while (have_posts()) : the_post(); ?>
        <!-- Standard post content -->
    
      <?php endwhile; ?>
    
    <?php endif;?>
    <!-- end if home page / else -->
      //pagination
    
    <!-- else if have no posts -->
    <?php else : ?>
      <?php _e('Sorry, no posts matched your criteria.'); ?>
    
    <?php endif; ?>
    <!-- end if have posts / else -->
    

    【讨论】:

      【解决方案4】:

      您可以使用此代码仅显示置顶帖子

      <?php
      $args = array(
      'posts_per_page' => 4,
      'post__in'  => get_option('sticky_posts'), //that will display only sticky posts
      );
      $my_query = new WP_Query($args);
      while($my_query->have_posts()) : $my_query->the_post();
      ?>
          <!-- sticky posts -->
      
      <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
      

      然后使用此代码从循环中仅删除置顶帖子

      <?php
      $args = array(
      'posts_per_page' => 4,
      'post__not_in' => get_option('sticky_posts') //that will remove only sticky posts
      );
      $my_query = new WP_Query($args);
      while($my_query->have_posts()) : $my_query->the_post();
      ?>
          <!-- normal posts content without sticky -->
      
      <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
      

      【讨论】:

        【解决方案5】:

        如果帖子很粘,只需在else 中输入continue ?

        <?php if ( is_sticky() ) continue;?>
        

        在其他部分的顶部...
        你的代码看起来像这样

        <?php if (have_posts()) : ?>
        <?php $post = $posts[0]; $c=0;?>
        <?php while (have_posts()) : the_post(); ?>
        
         <?php $c++;
           if( is_home() && !$paged && $c == 1 ) :?>
             <!--First sticky post content -->
        
        <?php elseif( is_home() && !$paged && $c == 2 ) :?>
            <!--Second sticky post content -->
        
        <?php elseif( is_home() && !$paged && $c == 3 ) :?>
            <!--Third sticky post content -->
        
        <?php else:?>
            <?php if ( is_sticky() ) continue;?>
            <!-- Standard post content -->
        
        <?php endif;?>
        <?php endwhile; ?>
        <!-- End of the main loop -->
          //pagination
        
        <?php else : ?>
          <?php _e('Sorry, no posts matched your criteria.'); ?>
        
        <?php endif; ?>
        

        【讨论】:

          猜你喜欢
          • 2017-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-04
          • 1970-01-01
          • 1970-01-01
          • 2013-10-29
          相关资源
          最近更新 更多