将循环输出保存到变量中
I have a loop to get a list of terms for a taxonomy.
<?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;
?>
The loop output is this:
'termname-one','termname-two','termname-three'
Now I want to save this output into variable ($termoutput) and insert it into a array of terms of following loop:
<?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)
)
)
) ); ?>
Is there a way to achive this? Thank you!
我有一个循环来获取分类法的术语列表。 p>
&lt;?php
$ terms = get_field('modell');
if($ terms):
$ total = count($ terms); \ n $ count = 1;
foreach($ terms as $ term):
?&gt;
'&lt;?php echo $ term-&gt; slug; ?&gt;'
&lt;?php
if($ count&lt; $ total){
echo',';
}
$ count ++;
endforeach;
endif;
?&gt;
code> pre>
循环输出为: p>
'termname-one','termname -two','termname-three'
code> pre>
现在我想将此输出保存到变量( $ termoutput strong>)并将其插入 以下循环的术语数组: p>
&lt;?php
query_posts(array(
'post_type'=&gt;'posttypename',
'posrs_per_page'=&gt; ; -1,
'orderby'=&gt;'title',
'order'=&gt;'ASC',
'tax_query'=&gt;数组(
array(
'taxonomy'=&gt; 'systems',
'field'=&gt;'slug',
'terms'=&gt; array($ termoutput)
)
)
)); ?&gt;
code> pre>
有没有办法实现这个目标? 谢谢! p>
div>
You should accumulate your output into an array like this:
$termoutput = array();
...
foreach( $terms as $term ) {
$termoutput[] = $term->slug;
}
Then, in the second section of your code:
...
'terms' => $termoutput
Try this:
<?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
)
)
) );
?>
This will store $term->slug to $termoutput[] as an array.