返回var在foreach循环外存储的所有值

返回var在foreach循环外存储的所有值

问题描述:

因此,我假设某些内容被覆盖,但我不确定如何停止此操作并检索循环外的所有值。有任何想法吗?返回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

首先初始化变量要以后使用:

$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

$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 

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.. 

$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); 

我试着上面的几个例子,他们没有按照我想要的方式工作。所以我绕了一圈,做了一些调查,这是我如何为我工作。

在我的特殊情况下,我需要获取特定帖子的所有类别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(); ?>