Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将循环输出保存到变量中_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

Php 将循环输出保存到变量中

Php 将循环输出保存到变量中,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我有一个循环来获取分类法的术语列表 <?php $terms = get_field('modell'); if( $terms ): $total = count($terms); $count = 1; foreach( $terms as $term ): ?> '<?php echo $term->slug; ?>' <?php if ($count <

我有一个循环来获取分类法的术语列表

    <?php 
  $terms = get_field('modell');
  if( $terms ):
    $total = count($terms);
    $count = 1;
    foreach( $terms as $term ):
      ?>
      '<?php echo $term->slug; ?>'
      <?php 
      if ($count < $total) {
        echo ', ';
      }
      $count++;
    endforeach;
  endif; 
?>
现在,我想将此输出保存到变量($termoutput)中,并将其插入到以下循环的术语数组中:

<?php 
query_posts( array(  
    'post_type' => 'posttypename', 
    'posts_per_page' => -1, 
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => array( 
       array( 
          'taxonomy' => 'systems', 
          'field' => 'slug', 
         'terms' => array($termoutput)
       ) 
   ) 

) );    ?>

有没有办法做到这一点?谢谢大家!

试试这个:

 <?php 
  $terms = get_field('modell');
  if( $terms ):
    $total = count($terms);
    $count = 1;
    $termoutput = array();
    foreach( $terms as $term ):

      echo "'".$term->slug."'"; 
      $termoutput[] = $term->slug;

      if ($count < $total) {
        echo ', ';
      }
      $count++;
    endforeach;
  endif; 
?>


<?php 
    query_posts( array(  
        'post_type' => 'posttypename', 
        'posts_per_page' => -1, 
        'orderby' => 'title',
        'order' => 'ASC',
        'tax_query' => array( 
          array( 
              'taxonomy' => 'systems', 
              'field' => 'slug', 
             'terms' => $termoutput
           ) 
       ) 

    ) );    
 ?>

这将$term->slug存储到$termoutput[]作为一个数组。

尝试以下操作:

 <?php 
  $terms = get_field('modell');
  if( $terms ):
    $total = count($terms);
    $count = 1;
    $termoutput = array();
    foreach( $terms as $term ):

      echo "'".$term->slug."'"; 
      $termoutput[] = $term->slug;

      if ($count < $total) {
        echo ', ';
      }
      $count++;
    endforeach;
  endif; 
?>


<?php 
    query_posts( array(  
        'post_type' => 'posttypename', 
        'posts_per_page' => -1, 
        'orderby' => 'title',
        'order' => 'ASC',
        'tax_query' => array( 
          array( 
              'taxonomy' => 'systems', 
              'field' => 'slug', 
             'terms' => $termoutput
           ) 
       ) 

    ) );    
 ?>


这将$term->slug存储到$termoutput[]作为一个数组。

您应该将输出累积到如下数组中:

$termoutput = array();

...

foreach( $terms as $term ) {
    $termoutput[] = $term->slug;
}
然后,在代码的第二部分:

...
'terms' => $termoutput

您应该将输出累积到如下数组中:

$termoutput = array();

...

foreach( $terms as $term ) {
    $termoutput[] = $term->slug;
}
然后,在代码的第二部分:

...
'terms' => $termoutput

$termoutput=[]在foreach之前。然后在循环中使用
$termoutput[]=$term->slug,而不是echo。。。就是这样。
$termoutput=[]在foreach之前。然后在循环中使用
$termoutput[]=$term->slug,而不是echo。。。确实如此。在某些情况下,这会引发警告,因为您没有在循环之前将
$termoutput
预设为数组。添加
$termoutput=array()在foreach之前。@nath开发人员很抱歉忘记了这一点,他正忙于编辑他的代码。谢谢,在某些情况下,这会引发警告,因为您没有在循环之前将
$termoutput
预设为数组。添加
$termoutput=array()在foreach之前。@nath开发人员很抱歉忘记了这一点,他正忙于编辑他的代码。谢谢你,太好了。对我来说很好。非常感谢。完美的对我来说很好。非常感谢。