2015-04-12 69 views
1

当前使用ACF Repeater for WP来显示某个类别中的某些帖子,但是如果我添加了同一个中继器,我希望它保留一个日志,记录已经使用后的id,因此它可以排除他们从新的循环。忽略已在循环中使用的wordpress中的帖子

唯一的问题是我当前的代码对于第一个循环和第二个循环都正常工作,但是添加的不止两个只是重置回第一组职位。转储数组看起来好像不会将数组添加到数组中。

第一阵列看起来像这样

array(3) { [0]=> int(28890) [1]=> int(28790) [2]=> int(28785) } 

二阵列

array(3) { [0]=> int(28749) [1]=> int(1) [2]=> int(28484) } 

array(3) { [0]=> int(28890) [1]=> int(28790) [2]=> int(28785) } 

这里是我的代码

<?php 
$cat = get_sub_field('category_name'); 
$args = array(
    'posts_per_page' => 3, 
    'category_name' => $cat, 
    'post__not_in' => $ids 
); 
query_posts($args); 
$ids = array(); 
?> 
<div class="hub-cont"> 
<?php while (have_posts()) : the_post(); ?> 
<?php array_push($ids,get_the_ID()); /*$ids[] = get_the_ID();*/?> 
    <div class="blockitem2 small-12 medium-4 large-4"> 
     <?php 
     // Fetch all posts relating to a certain tag then display 4 of them 
     //Get the Thumbnail URL 
     $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(720,405), false, ''); 
     ?> 

     <div id="promolink"></div><div class="blockimage" style="background-image: url('<?php echo $src[0]; ?>'); background-repeat: no-repeat; background-size: cover;"> 

      <div class="cats"><?php echo the_category(' '); ?></div> 
     </div> 
     <div class="meta"> 
      <a class="gdbnewslink dark" href="<?php echo get_permalink();?>" ><?php the_title();?> </a> 
     </div> 
     <div class="clear"></div> 
     <div id="newsintro"><?php $text = $post->post_content; $trimmed = wp_trim_words($text, 50, null); echo $trimmed; ?></div> 
    </div> 

<?php endwhile; ?> 
    <?php wp_reset_query(); ?> 
    <?php var_dump($ids); ?> 
</div> 

阵列对我来说仍然很新,所以您的指导将非常感谢!

+0

新回路从哪里开始?新循环是否调用完全相同的代码,那么它将不起作用,因为您将'$ ids'重置为空数组。 – Luceos

+0

是的,循环再次是相同的代码。我认为这是重置数组,我怎么能够创建可重复的代码,而不重置数组? – Kyon147

回答

1

以下是使用此链接信息的解决方案。 https://www.binarymoon.co.uk/2010/03/5-wordpress-queryposts-tips/

添加到您的功能文件。

$bmIgnorePosts = array(); 

/** 
* add a post id to the ignore list for future query_posts 
*/ 
function bm_ignorePost ($id) { 
    if (!is_page()) { 
     global $bmIgnorePosts; 
     $bmIgnorePosts[] = $id; 
    } 
} 

/** 
* reset the ignore list 
*/ 
function bm_ignorePostReset() { 
    global $bmIgnorePosts; 
    $bmIgnorePosts = array(); 
} 

/** 
* remove the posts from query_posts 
*/ 
function bm_postStrip ($where) { 
    global $bmIgnorePosts, $wpdb; 
    if (count($bmIgnorePosts) > 0) { 
     $where .= ' AND ' . $wpdb->posts . '.ID NOT IN(' . implode (',', $bmIgnorePosts) . ') '; 
    } 
    return $where; 
} 

add_filter ('posts_where', 'bm_postStrip'); 

然后利用这一点,你会做你的循环正常,并称之为“bm_ignorePost($后> ID);”每篇文章要忽略。以下示例使用相同的查询两次,但会在每个输出上显示完全不同的帖子。

<?php 
// set the query 
$query = 'posts_per_page=10'; 

// loop 1 - display most recent 10 posts 
$queryObject = new WP_Query($query); 
if ($queryObject->have_posts()) { 
    while ($queryObject->have_posts()) { 
     bm_ignorePost($queryPost->post->ID); 
     $queryObject->the_post(); 
     the_title(); 
     the_content(); 
    } 
} 

// loop 2 - same query, get the next 10 posts 
$queryObject = new WP_Query($query); 
if ($queryObject->have_posts()) { 
    while ($queryObject->have_posts()) { 
     bm_ignorePost($queryPost->post->ID); 
     $queryObject->the_post(); 
     the_title(); 
     the_content(); 
    } 
} 
?>