Unsticky一些粘贴的帖子

时间:2012-12-20 19:15:31

标签: php wordpress sticky

我有2个循环。在第一个循环中,显示了3个最近的粘贴帖子。在第2个帖子中显示所有帖子(粘性和非粘性)。现在我想删除第二个循环中最近的3个粘贴帖子。

我尝试了类似这样的东西,但它从两个循环中删除了所有粘性帖子,而不是从loop2中删除了一些粘性帖子。 //编辑:我的两个循环的代码:

<?php
$sticky = get_option('sticky_posts'); // Get all sticky posts.
rsort($sticky); // Sort sticky posts in reverse order.
$sticky = array_slice($sticky, 0, 3); // Number of sticky posts to show.
$latestStickyPosts = $sticky;

$loop1query = new WP_Query(array('post__in' => $sticky, 'ignore_sticky_posts' => 1));

// START 1ST LOOP.
while ($loop1query->have_posts()) : $loop1query->the_post();
$do_not_duplicate[] = $post->ID; // global. ?>
<div> ... </div>
<?php endwhile;
// END 1ST LOOP.


$showposts = 5; // Show posts per page. 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'paged' => $paged);

$loop2query = new WP_Query($args);

// START 2ND LOOP.
query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); 
if( in_array($post->ID, $latestStickyPosts ) continue; ?>
<div> ... </div>
<?php endwhile; endif;
// END 2ND LOOP. ?>

我不在乎这些帖子在后端是否真的不粘。它们应该以这种方式显示:loop1 = 3最近的胶粘物。 loop2 =其他的胶粘物和所有不粘的帖子

感谢您的帮助或建议!

1 个答案:

答案 0 :(得分:0)

我没有在你的代码中看到第一个循环,但是由于你循环遍历第二个循环中的所有帖子,你可以保留一个数组,其中包含你不需要显示的粘贴帖子的3个ID试。

假设你有一个数组$latestStickyPosts,那么你可以这样做:

query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); 
if (in_array($post->ID, $latestStickyPosts)) continue; ?>

有关详情,请查看this documentation

相关问题