无法从wp_Dropdown_Categories或Get_Categories获取选定值

时间:2022-01-04 作者:ratib90486

我已经搜索并找到了this 和afew more. 但我仍然无法获取所选名称(不是value,因为value只是一个数字)。get\\u categories keeps返回空数组。

下面是我正在修改的插件的代码。

$categories = apply_filters( \'acadp_admin_listing_form_categories_dropdown\', \'\', $post->ID );

        if ( empty( $categories ) ) {
            $selected_category = count( $category ) ? $category[0] : -1;
            
            $args = array(
                \'show_option_none\' => \'-- \' . esc_html__( \'Select category\', \'advanced-classifieds-and-directory-pro\' ) . \' --\',
                \'taxonomy\'         => \'acadp_categories\',
                \'name\'             => \'acadp_category\',
                \'orderby\'          => \'name\',
                \'selected\'         => (int) $selected_category,
                \'hierarchical\'     => true,
                \'depth\'            => 10,
                \'show_count\'       => false,
                \'hide_empty\'       => false,
                \'echo\'             => false,
            );
            
            if ( $disable_parent_categories ) {
                $args[\'walker\'] = new ACADP_Walker_CategoryDropdown;
            }
            
            $categories = wp_dropdown_categories( $args );
        }

        echo $categories; 

1 个回复
SO网友:ratib90486

我想出来了。要获取所选类别,我不需要上面的所有代码。我只需要下面的。

$category = wp_get_object_terms( $post->ID, \'acadp_categories\' );
$selected_category = $category[0]->name;