Wordpress从循环中排除重复的$ sticky帖子

时间:2016-08-13 20:30:07

标签: php wordpress loops

由于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; ?>

结果是我获得了自定义样式的前三个帖子(选中为粘性),同时在标准帖子输出中重复,但我没有将其删除。

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

非常感谢任何使重复帖子不重复的帮助。

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

处理粘贴帖子

<?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())之类的操作。

答案 2 :(得分:1)

如果帖子是粘性的,只需continue else

<?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; ?>

答案 3 :(得分:1)

您可以使用此代码仅显示粘贴帖子

<?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(); ?>
相关问题