2012-01-07 62 views
1

因此,我假設某些內容被覆蓋,但我不確定如何停止此操作並檢索循環外的所有值。有任何想法嗎?返回var在foreach循環外存儲的所有值

foreach($gallids as $gallterm) 
{ 
    $postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")); 

    $checkmissing = $postterms[0];     
    print_r($checkmissing); // Check Terms for missing images - works here. 
} 

print_r($checkmissing); // Check Terms for missing images - not working here 
         // - seems to be getting last or first val only. 
+0

要麼使$ checkmissing陣列,或使用$ checkmissing = $ postterms [0];將每個postterms值連接到字符串...或$ checkmissing。=','。 $ postterms [0];如果你想逗號分隔的字符串 – 2012-01-07 10:57:15

回答

4

首先初始化變量要以後使用:

$checkmissing = array(); 

然後foreach內追加後條款的該數組中的第一項:

foreach($gallids as $gallterm) 
{ 
    list($checkmissing[]) = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")); 
} 

請參閱$checkmissing[],這將有效防止您覆蓋它。它將每個追加到陣列。

終於可以輸出循環之後的結果:

print_r($checkmissing); 

注:你應該做一些額外的處理,如果wp_get_post_terms返回一個空數組:

foreach($gallids as $gallterm) 
{ 
    $terms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")) 
     AND list($checkmissing[]) = $terms 
    ; 
} 
+0

賓果。謝謝@hakre – user1134561 2012-01-07 11:39:02

0
$checkmissing = array(); 

foreach($gallids as $gallterm) 
{ 
    $postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")); 

    $checkmissing[] = $postterms[0];     
    print_r($checkmissing); // Check Terms for missing images - works here. 
} 

print_r($checkmissing); // Check Terms for missing images 
         // will get all missing images as array 
0
foreach($gallids as $gallterm) { 

     $postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")); 
     $checkmissing[] = $postterms[0]; 
    } 

    print_r($checkmissing); //Now this will be a 2d array with all your values.. 
0
$checkmissing = array(); 
    $i=1; 
    foreach($gallids as $gallterm) 
    { 
     $postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")); 

     $checkmissing[$i] = $postterms[0];     
     //print_r($checkmissing); // Check Terms for missing images - works here. 
    $i++; 
    } 

    print_r($checkmissing); 
0

我試着上面的幾個例子,他們沒有按照我想要的方式工作。所以我繞了一圈,做了一些調查,這是我如何爲我工作。

在我的特殊情況下,我需要獲取特定帖子的所有類別ID,然後將該變量返回到WP_Query參數數組中。

首先,你需要抓住後條款

$terms = get_the_terms($post->ID, 'category'); 

接下來你要初始化你希望以後使用的變量:

$cat_terms = array(); 

接下來,您將聲明foreach以獲得每個單獨的術語ID

foreach ($terms as $term) { 
    $cat_terms[] = $term->term_id; 
} 

現在讓我們假設您要使用返回一個逗號分開d這個$ cat_terms變量的列表。我們將使用「加入」功能

$comma_separated_terms = join(", ", $cat_terms); 

現在,假設你想要說「category__in」參數來使用這個變量來投入你WP_Query循環。我們將使用'array_values'。

$values = array_values($cat_terms); 

關於這樣做的好處是,現在我們可以將這個$值變量到WP_Query參數:

<?php global $post; 
     $query = new WP_Query(array(
      'post_type' => 'post_type_name', 
      'category__in' => $values)); 
?> 

在我的特殊情況下,客戶想一些自定義文章類型在側邊欄顯示基於博客帖子類別。因此,我需要獲取所有博客帖子條款,並將其與自定義帖子類型類別的條款進行匹配。

最終代碼看起來是這樣的:

<?php 
    $terms = get_the_terms($post->ID, 'category'); 

    $cat_terms = array(); 

    foreach ($terms as $term) { 
     $cat_terms[] = $term->term_id; 
    } 

    $values = array_values($cat_terms); 
?> 
    <h3><?php echo $title; ?></h3> 

    <?php global $post; 
     $query = new WP_Query(array(
      'post_type' => 'custom_post_type', 
      'category__in' => $values)); 

      if ($query->have_posts()) : ?> 

       <?php while ($query->have_posts()) : $query->the_post(); ?> 

       // Code for loop goes here 

       <?php endwhile; endif; ?> 

      <?php wp_reset_postdata(); ?>