我想在搜索参数中使用循环,如下所示
$community = array("eleven_polls", "activities_coordinator", "attractively_priced_onsite_laundry_care_centers");
$community_count = count($community);
$args = array(
'numberposts' => -1,
'post_type' => 'apartment',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'AND',
for ($i=0; $i < $community_count ; $i++) {
array(
'key' => 'community',
'value' => $_GET['community'][$i],
'compare' => 'LIKE'
),
}
)
)
);
但它在循环线上显示语法错误,如何在数组中运行循环
for ($i=0; $i < $community_count ; $i++) {
$array = array(
'key' => 'community',
'value' => $_GET['community'][$i],
'compare' => 'LIKE'
),
}
$args = array(
'numberposts' => -1,
'post_type' => 'apartment',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'AND',
$array
)
)
);
我不明白你想要达到什么目的。但试试这种方式。
不要在数组内循环。如下所示。
$community = array("eleven_polls", "activities_coordinator", "attractively_priced_onsite_laundry_care_centers");
$community_count = count($community);
$args = array(
'numberposts' => -1,
'post_type' => 'apartment',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'AND',
)
)
);
for ($i=0; $i < $community_count ; $i++) {
$args = array(
'key' => 'community',
'value' => $_GET['community'][$i],
'compare' => 'LIKE'
);
}
希望,它会像你要找的那样工作。
echo '<pre>';
print_r($args);
echo '</pre>';
Array
(
[numberposts] => -1
[post_type] => apartment
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[relation] => AND
[0] => Array
(
[key] => community
[value] => Array
(
[0] => 0
)
[compare] => LIKE
)
[1] => Array
(
[key] => community
[value] => Array
(
[0] => 1
)
[compare] => LIKE
)
[2] => Array
(
[key] => community
[value] => Array
(
[0] => 2
)
[compare] => LIKE
)
)
)
)