通过While循环重复数组内的数组

时间:2018-07-28 作者:sultan

我要重复此数组:

array(
    \'taxonomy\' => $tax,
    \'field\'    => \'slug\', 
    \'terms\'    => $cat,
)
因为我从自定义子字段中获取分类法和类别的值

$args = array( \'post_type\' => \'watches\',  \'tax_query\' => array(
    array(
        \'taxonomy\' => $tax,
        \'field\'    => \'slug\', 
        \'terms\'    => $cat,
        ),
    array(
        \'taxonomy\' =>  $taxonomy_2,
        \'field\'    => \'slug\',
        \'terms\'    => $cat_2,
        ),
    array(
        \'taxonomy\' =>  $taxonomy_3,
        \'field\'    => \'slug\',
        \'terms\'    => $cat_3,
        ),
    ),
);

1 个回复
SO网友:Jacob Peattie

从创建参数开始,但离开tax_query 空:

$args = array(
    \'post_type\' => \'watches\',
    \'tax_query\' => array(),
);
然后在循环中可以添加到tax_query具有$args[\'tax_query\'][] =:

$args = array(
    \'post_type\' => \'watches\',
    \'tax_query\' => array(),
);

foreach ( $things as $thing ) {
    $args[\'tax_query\'][] = array(
        \'taxonomy\' => $thing[\'taxonomy\'],
        \'field\'    => \'slug\', 
        \'terms\'    => $thing[\'term\'],
    );
}
你循环的内容可能会影响$things 以及你会如何使用它,但你的问题中没有包含这些信息,所以除了我已经提出的内容之外,我无能为力。

结束