【问题标题】:Return all values stored in var outside foreach loop在foreach循环之外返回存储在var中的所有值
【发布时间】:2012-02-04 20:21:09
【问题描述】:

所以我假设某些内容正在被覆盖,但我不确定如何停止此操作并检索循环外的所有值。有什么想法吗?

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.

【问题讨论】:

  • 要么使 $checkmissing 成为一个数组,要么使用 $checkmissing .= $postterms[0];将每个 postterms 值连接到字符串...或 $checkmissing .= ', ' 。 $postterms[0];如果你想要一个逗号分隔的字符串

标签: php arrays variables loops foreach


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我尝试了上面的一些示例,但它们并没有按照我想要的方式工作。所以我四处寻找并做了一些研究,这就是我如何让它为我工作。

    在我的特殊情况下,我需要获取特定帖子的所有类别 ID,然后将该变量返回到 WP_Query 参数数组中。

    首先,您需要获取帖子条款

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

    接下来,您需要初始化稍后要使用的变量:

    $cat_terms = array();
    

    接下来,您将声明 foreach 以获取每个单独的术语 ID

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

    现在假设您要为这个 $cat_terms 变量使用返回一个逗号分隔的列表。我们将使用“加入”功能

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

    现在假设您想使用此变量将“category__in”参数放入 WP_Query 循环中。我们将使用“array_values”。

    $values = array_values($cat_terms);
    

    这样做的好处是现在我们可以将这个 $values 变量插入到 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(); ?> 
    

    【讨论】:

      【解决方案3】:
      $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
      

      【讨论】:

        【解决方案4】:
        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..
        

        【讨论】:

          【解决方案5】:
          $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);
          

          【讨论】:

            猜你喜欢
            • 2014-08-31
            • 2021-12-25
            • 2022-10-14
            • 1970-01-01
            • 1970-01-01
            • 2019-10-09
            • 1970-01-01
            • 1970-01-01
            • 2014-06-08
            相关资源
            最近更新 更多