2015-04-12 72 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(); 
    } 
} 
?>