如何在选择下拉菜单中的WordPress选项中使用Redux框架获取产品列表

8xiog9wr  于 2023-03-30  发布在  WordPress
关注(0)|答案(2)|浏览(104)

我正在使用redux框架,我想使用select部分作为drpwdown并显示产品列表,但我不知道如何调用产品?Show products in the dropdown list

$fields = array(
        'id' => 'creations_pro_id',
        'type' => 'select',
        'data'     => 'post_type',
        'args'      => array(
            'post_type' => 'product',
        )
    ),
mtb9vblg

mtb9vblg1#

create function()函数名:

function my_callback_function()
{
    $items = array();

    $args = array(
        'post_type' => 'product',
        'status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'limit' => -1,
    );
    $products = wc_get_products($args);
    if (count($products) > 0) {
        foreach ($products as $product) {
            $items[] = $product->get_name() . ' ['.$product->get_id().']' ;
        }
        return $items;
    }
}

然后使用下面的函数:

$fields = array(
    'id'    => 'opt-button-set-term',
    'type' => 'select',
    'data' => 'callback',
    'args' => 'my_callback_function',
);

这段代码工作正常!

0lvr5msh

0lvr5msh2#

这是另一种方式:

$fields = array(
    'id'    => 'opt-select-post',
    'type'  => 'select', 
    'data'  => 'posts',
    'args'  => array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'orderby'        => 'title',
        'order'          => 'ASC',
    )
);

这种方法也能正确工作

相关问题