2014-03-12 101 views
0

在Wordpress中,我試圖在循環中顯示數據。在這個特殊的環路我想包括具有以下元數據的所有項目:顯示來自同一陣列,具有不同元數據的多個項目

red 
blue 
green 

我有以下陣列:

 $posts = get_posts(array(
    'numberposts' => -1, 
    'post_type' => 'people', 
    'meta_key' => 'role', 
    'meta_compare' => '=', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_value' => 'red', 
    ) 
); 

這顯示是red就好項目。但如何添加到數組,以便它顯示所有項目?

的功能代碼是在這裏:

 if($posts) 
      {  ?> 

     <h3 class="col-md-12">Red & Blue & Green</h3> 
    <?php 
      foreach($posts as $post) 
      { 
       ?>   
       <div class="thumb-frame col-md-3 col-xs-6 pull-right"> 
        <a href="#detail<?php echo get_the_id(); ?>" onClick="showDetail('<?php echo get_the_id(); ?>');"> 
        <figure class="thumb"> 
         <img class="img-responsive" src="<?php the_field('portrait_image');?>" alt="<?php the_title(); ?>"/> 
         <figcaption> 
         <span><?php the_title(); ?></span> 
         </figcaption> 
        </figure> 
        </a> 
       </div> 
       <?php 
     $num++; 
    }   
      }  
     ?> 

我新的PHP和我不能確定的語法,但應該有一個OR操作,或者嵌套數組?

+0

你提到你有很多在下面的評論中循環。閱讀[**什麼時候應該使用WP_Query vs query_posts()vs get_posts()?**](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-查詢帖子vs獲取帖子) – brasofilo

回答

0

我不是一個WordPress的開發者,但我猜你可以通過檢索3套職位從數據庫中獲取這些帖子:

$posts = get_posts(array(
    'numberposts' => -1, 
    'post_type' => 'people', 
    'meta_key' => 'role', 
    'meta_compare' => '=', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_value' => 'red', 
)); 

$posts += get_posts(array(
    'numberposts' => -1, 
    'post_type' => 'people', 
    'meta_key' => 'role', 
    'meta_compare' => '=', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_value' => 'blue', 
)); 

$posts += get_posts(array(
    'numberposts' => -1, 
    'post_type' => 'people', 
    'meta_key' => 'role', 
    'meta_compare' => '=', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_value' => 'green', 
)); 

現在$posts數組將包含的3聯盟結果。

+0

感謝您的嘗試,但$ post變量只是被數組的每次迭代覆蓋 –

+0

嘿@PeteNorris,不是它不會,你需要使用$ posts + =而不是$ posts =。這是一個陣列上的基本PHP運算符,試試吧。 –

+0

啊!對不起,讓我給它一個........ –

0

嘗試

$posts = get_posts(array(
    'numberposts' => -1, 
    'post_type' => 'people', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_query' => array(
     'key'=>'role', 
     'value'=>array('red', 'blue', 'green'), 
     'compare'=>'OR' 
    ) 

) 
); 

就以WP_Query

+0

這是返回的一切...甚至項目的價值觀'黃色''紫色'等 –

0

使用更改查詢試試吧一看,它可以幫助你

query_posts(array(
    'post_type' => 'people', 
    'meta_query' => array(
     array(
      'key' => 'role', 
      'value' => 'red', 
      'compare' => 'LIKE' 
     ), 
     array(
      'key' => 'role', 
      'value' => 'blue', 
      'compare' => 'LIKE' 
     ) 
    ) 
)); 

Reference from scribu

<?php 
if (have_posts()) { 
    echo "<h3 class="col-md-12">Red & Blue & Green</h3>"; 
    while (have_posts()) { 
     the_post(); 

     **// Your loop code** 

    } 
else { 
    echo wpautop('Sorry, no posts were found'); 
} 
?> 
+0

這是有道理的,但我不能讓它返回任何東西。 –

+0

@PeteNorris答案編輯。嘗試替換循環。 –

+0

謝謝嘗試 - 「對不起,沒有發現帖子」 –

相關問題