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

时间:2017-03-29 11:55:09

标签: php wordpress visual-composer

我正在尝试为Visual Composer制作一个短代码。我必须将所有自定义帖子类型作为下拉列表。我正在使用@ConfigurationProperties函数,但它返回一个空数组。

这是我的代码:

get_post_types()

}

我也试过在functions.php中得到它,但结果是一样的。

我也使用 /*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, ), ) ) ); ,它在钩子内工作,但不在钩子之外。

任何人都能帮帮我吗?

2 个答案:

答案 0 :(得分:2)

尝试使用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,
            ),
        )
    ) );

}

答案 1 :(得分:0)

我已根据您为短代码值调用$ 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
            ),
        )
    ) );
}

此行应返回已注册的帖子类型的名称。希望这对你有用!