【问题标题】:How to use loop inside array of arguments in WordPress如何在WordPress的参数数组中使用循环
【发布时间】:2018-08-12 17:40:03
【问题描述】:

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

$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 循环行显示语法错误,我如何在数组内运行 for 循环

【问题讨论】:

  • 不能在数组中执行循环
  • 但是如何实现我想要的,你有什么想法吗? @Mr.Developer
  • 您应该在数组关闭后删除逗号 (,)。

标签: php arrays wordpress for-loop


【解决方案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

            )
        )
    );

我不明白你想达到什么目的。但是试试这个方法。

【讨论】:

    【解决方案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++) { 
                   $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
                            )
    
                    )
    
            )
    
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2012-02-09
      相关资源
      最近更新 更多