如何在 wordpress 中获取所有自定义 post 类型?

How to get all custom post types in wordpress?

我正在尝试为 Visual Composer 制作简码。我必须将所有自定义 post 类型作为下拉列表。我正在使用 get_post_types() 函数,但它 returns 是一个空数组。

这是我的代码:

 /*Post type shortcode*/
 add_action( 'vc_before_init', 'post_type_shortcode');
function post_type_shortcode(){
$args = array( 'public' => true, '_builtin' => false );
$output = 'names'; //'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$custom_post_types = get_post_types( $args, $output, $operator );
vc_map( array(
    "name" => __( "Display Post Type", "saue" ),
    "description" => "Display post type",
    "base" => "display_post_type",
    "class" => "",
    "category" => __( "Saue Theme", "saue"),
    "params" => array(
        array(
            "type"          => "dropdown",
                    //"holder"        => "div",
            "heading"       => __( "Post Type", "saue" ),
            "admin_label" => true,
            "param_name"    => "post_type",
            "value"         => $custom_post_types,
            ),
        )
    ) );

}

我也尝试在 functions.php 中获取它,但结果是一样的。

我也用过add_action('init',"function_name');,钩子内有效,钩子外无效。

有人可以帮我吗?

我已经围绕您为短代码值调用 $custom_post_types 的行修改了您的代码。

/*Post type shortcode*/
add_action( 'vc_before_init', 'post_type_shortcode');
function post_type_shortcode(){
$args = array( 'public' => true, '_builtin' => false );
$output = 'names'; //'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$custom_post_types = get_post_types( $args, $output, $operator );
vc_map( array(
    "name" => __( "Display Post Type", "saue" ),
    "description" => "Display post type",
    "base" => "display_post_type",
    "class" => "",
    "category" => __( "Saue Theme", "saue"),
    "params" => array(
        array(
            "type"          => "dropdown",
            //"holder"      => "div",
            "heading"       => __( "Post Type", "saue" ),
            "admin_label"   => true,
            "param_name"    => "post_type",
            "value"         => $custom_post_types->name,//see this line
            ),
        )
    ) );
}

这一行应该 return 注册的 post 类型的名称。希望这对你有用!

尝试使用 admin_init 挂钩,它在 init:

之后运行
/*Post type shortcode*/
add_action( 'admin_init', 'post_type_shortcode');
function post_type_shortcode(){
$args = array( 'public' => true, '_builtin' => false );
$output = 'names'; //'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$custom_post_types = get_post_types( $args, $output, $operator );
vc_map( array(
    "name" => __( "Display Post Type", "saue" ),
    "description" => "Display post type",
    "base" => "display_post_type",
    "class" => "",
    "category" => __( "Saue Theme", "saue"),
    "params" => array(
        array(
            "type"          => "dropdown",
                    //"holder"        => "div",
            "heading"       => __( "Post Type", "saue" ),
            "admin_label" => true,
            "param_name"    => "post_type",
            "value"         => $custom_post_types,
            ),
        )
    ) );

}