Php 将城市分类法列为woocommerce chekout shipping city下拉列表

Php 将城市分类法列为woocommerce chekout shipping city下拉列表,php,arrays,wordpress,woocommerce,Php,Arrays,Wordpress,Woocommerce,我已经通过以下方法添加了自定义的checkout city下拉字段 $fields['shipping']['shipping_city'] = array( 'label' => __('City', 'woocommerce'), 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => true, 'clear' => tru

我已经通过以下方法添加了自定义的checkout city下拉字段

$fields['shipping']['shipping_city'] = array(
    'label'       => __('City', 'woocommerce'),
    'placeholder' => _x('', 'placeholder', 'woocommerce'),
    'required'    => true,
    'clear'       => true,
    'type'        => 'select',
    'class'       => array('own-css-name'),
    'options'     => array(
        'New_York_City' => __('New York City', 'woocommerce' ),
        'Chicago' => __('Chicago', 'woocommerce' ),
        'Dallas' => __('Dallas', 'woocommerce' )
        )
    );
我想从city taxonomy中获取选项值,因此我尝试了以下方法,但似乎不起作用()

试试这个

只要换个新的

  • 循环使用

    foreach($categories作为$category){ $cityArray[$category->slug]=$category->name; }

  • 像这样设置选项

    “选项”=>数组_值($cityArray)

已对其进行了测试,结果如下所示


让我知道这是否对您有效。

它有效,但它生成的选项值为0,1,2,而不是类别slug它生成的纽约市芝加哥达拉斯我需要的是纽约市芝加哥达拉斯,只需写下以下声明var_dump($cityArray);foreach循环结束后,请用括号括起来,并让我知道签出页面上的输出,如果var_dump语句在签出页面数组(size=3)“New_York_City”=>字符串“New York City”(长度=13)“Chicago”=>字符串“Chicago”(长度=7)“Dallas”=>字符串“Dallas”(长度=6)然后在您的示例中,将“选项”=>array\u值($cityArray)替换为“选项”=>$cityArray”
    $args = array( 
     'child_of' => 0,
     'type'     => 'product',
     'taxonomy' => 'city',
     'hide_empty' => 0
     ); 
    $categories = get_categories( $args );
    foreach ($categories as $category) {
    $cityArray[] = "'".$category->slug."' => __('".$category->name."', 'woocommerce' )";
    }
    $fields['shipping']['shipping_city2'] = array(
        'label'       => __('City', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required'    => true,
        'clear'       => true,
        'type'        => 'select',
        'class'       => array('own-css-name'),
        'options'     => array(implode( ', ', $cityArray ) )
 );