2017-08-16 7 views
0

Wordpressで使用しているテーマを展開するコードを書き込もうとしています。カスタム投稿を取得し、オプション配列に入れます - wordpressプラグイン

基本的には、すべてのカスタムポストタイプを取得して選択用の配列に入れたいのですが、問題は配列にオプション値を追加する必要があり、foreachループを配列のようにこれを行う方法がわからない。

'options'   => array(), 

カスタムポストは形式にする必要がある場所です:あなたは、コードが表示されます次のコードで

function get_fields() {   

    $fields = array(
        'get_post_names' => array(
      'label'   => esc_html__('Url Opens', 'builder'), 
      'type'   => 'select', 
      'option_category' => 'configuration', 
      'options'   => array(), 
      'toggle_slug'  => 'post_names', 
      'description'  => esc_html__('Here you can choose whether or not your link opens in a new window', 'et_builder'), 
      ), 
    ); 

     global $wpdb; 
     $custom_post_type = 'custom_post_name'; 
     $results = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type), ARRAY_A); 
     if (! $results) 
      return; 

     foreach($results as $index => $post) { 
     $fields['options'][] = array (
      $post['ID'] => esc_html__($post['post_title'], 'builder'), 
     ); 
     } 

      return $fields; 

     } 
:ここ

'PostID' => esc_html__('Post Name', 'builder'), 

は私のコードです

ご協力いただければ幸いです。

答えて

0

うまくいけば、これは

function generate_post_select($select_id, $post_type, $selected = 0) { 
     $post_type_object = get_post_type_object($post_type); 
     $label = $post_type_object->label; 
     $posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1)); 
     foreach ($posts as $post) { 
      echo $post->post_title; 
     } 
    } 

$select_idを働くかもしれ

おかげでは選択の名前とIDとして使用され、$post_typeは、あなたが選択にすることにしたいタイプで、$selectedはポストIDです選択ボックスで選択します。

+0

申し訳ありませんが、配列にありません –

0

誰かが知りたいと思えば解決策が見つかりました。

「オプション」を次のように変更し、グローバル$ wpdbからコードを削除しました。ダウン。

'options'   => array_reduce(get_posts('post_type=custom_post&posts_per_page=-1'), function($result, $item) { 
     $result[$item->ID] = $item->post_title; 
     return $result; 
    }), 
関連する問題