2017-03-29 8 views
0

私はVisual Composerのショートコードを作成しようとしています。すべてのカスタム投稿の種類をドロップダウンとして取得する必要があります。私はget_post_types()関数を使用していますが、空の配列を返します。ここでどのようにWordPressのすべてのカスタム投稿の種類を取得するには?

が私のコードです:

/*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');を使用しましたが、フック内で動作しますが、フック外では動作しません。

誰でも私を助けてくれますか?

+0

それは 'init'アクションを実行したときに動作しますか? –

+0

正常に動作します。 'add_action( 'init'、 'getPostTypes'); 関数getPostTypes() { $ post_types = get_post_types(); var_export($ post_types); } '' return $ post_types'を使用すると、デフォルトの投稿タイプのみが返されます。 –

答えて

0

は、initの後に実行admin_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, 
      ), 
     ) 
    )); 

} 
+0

ありがとう@Leo作品。 –

+0

あなたは大歓迎です:) –

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 
      ), 
     ) 
    )); 
} 

この行には、登録されている投稿タイプの名前が返されます。これはあなたのために働くことを望みます!

+0

これは、通知: 'Notice:E:\ xampp \ htdocs \ ... \ function.php'内の非オブジェクトのプロパティを取得しようとしていて、 'get_post_types'関数はこのフックに何も返しません。 :( –

関連する問題