0

公開されているすべての投稿、ページ、およびすべてのカスタム投稿タイプにカスタム フィールドをロードしようとしています。

しかし、それを組み合わせる方法がわかりません。

これは、投稿とページにカスタムフィールドをロードするために設定した変数です

public function vf_add_meta_box( $post_type ) {

    if (in_array( $post_type, $this->post_types )) {
        add_meta_box( 
            $this->option_name,
            $this->metabox_name,
            array( $this, 'meta_box_display' ),
            $post_type,
            'normal',
            'high'
        );
    }
}

$post_type は別の関数で定義して __construct に挿入し、このように設定します

$post_types = array('post', 'page' );

また、wordpress codex の手順から、このコードを使用してすべてのカスタム投稿タイプを取得できます。

$args = array(
   'public'   => true,
   '_builtin' => false
);

$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'

$custom_types = get_post_types( $args, $output, $operator ); 

foreach ( $custom_types  as $post_type ) {

   return $post_type ;
}

だから私は $post_types 配列に $post_type を追加すると仮定しました

$post_types = array('post', 'page', $post_type );

カスタム投稿タイプでメタボックスを表示しますが、機能しません。

4

1 に答える 1

1

最後の foreach を次のように置き換えてみてください。

$post_types = array_merge($post_types, $custom_types);

これにより、カスタム投稿タイプの配列とデフォルトの$post_types配列が結合されます。

于 2014-01-14T13:37:14.333 に答える