我有以下代码:
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => array( "usa", "canada" )
)
)
) );
我上面也有一个循环来标识所有这样的“术语”:
$term_list = get_the_terms($post->ID,$taxonomy);
foreach($term_list as $term_single) {
echo $term_single->slug; //do something here
}
我想用我的foreach循环的结果动态替换“美国”和“加拿大”。我该怎么办?
答案 0 :(得分:0)
您应该做类似的事情
function getTermsForQuery($post) {
$result = [];
$term_list = get_the_terms($post->ID,$taxonomy);
foreach($term_list as $term_single) {
$result[] = $term_single->slug; //do something here
}
return $result;
}
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => getTermsForQuery($post)
)
)
) );
答案 1 :(得分:0)
没有foreach
循环
$term_list = get_the_terms($post->ID,$taxonomy);
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => $term_list
)
)
) );
具有foreach
循环
$term_list = get_the_terms($post->ID,$taxonomy);
foreach($term_list as $term_single) {
$cat_terms[] = $term_single;
}
query_posts( array(
"tax_query" => array(
array(
"taxonomy" => "country",
"field" => "slug",
"terms" => $cat_terms
)
)
) );
答案 2 :(得分:0)
我创建了3个数组,然后填充每个数组并插入另一个数组。
//for loop, store the values that you want
$subArray2 = array();
for($row = 0;$row < 3;$row++){
$subArray2[$row] = "Value_$row";
}
$subArray1 = array("taxonomy" => "country","field" => "slug","terms" => $subArray2);
$arr = array("tax_query" => $subArray1);
//var_dump($arr);
foreach($arr as $val){
foreach($val as $val1){
if(is_array($val1)){
foreach($val1 as $val2){
echo "$val2<br>";
}
}else{
echo $val1 . "<br>";
}
}
}