如何在WordPress中的参数数组中使用循环

问题描述 投票:1回答:2

我想在搜索参数中使用循环,如下所示

$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'
                ),
            }
        )
    )
);

但它在循环线上显示语法错误,如何在数组中运行循环

php arrays wordpress for-loop
2个回答
1
投票
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

            )
        )
    );

我不明白你想要达到什么目的。但试试这种方式。


1
投票

不要在数组内循环。如下所示。

$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
                        )

                )

        )

)
© www.soinside.com 2019 - 2024. All rights reserved.