2012-12-16 11 views
9

でカスタムポストタイプに複数のメタボックスを追加:私の質問は私はポスト編集ウィンドウに管理ダッシュとカスタムメタボックスにカスタムポストタイプを追加し、次のコード持っているワードプレス

function teasers_custom_init() { 
    $labels = array(
    'name' => 'Teasers', 
    'singular_name' => 'Teaser', 
    'add_new' => 'Add New', 
    'add_new_item' => 'Add New Teasers', 
    'edit_item' => 'Edit Teaser', 
    'new_item' => 'New Teaser', 
    'all_items' => 'All Teasers', 
    'view_item' => 'View Teaser', 
    'search_items' => 'Search Teasers', 
    'not_found' => 'No teasers found', 
    'not_found_in_trash' => 'No teasers found in Trash', 
    'parent_item_colon' => 'Parent Page', 
    'menu_name' => 'Teasers' 
); 

    $args = array(
    'labels' => $labels, 
    'description' => 'set slider panels with loop times', 
    'public' => true, 
    'publicly_queryable' => true, 
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true, 
    'rewrite' => array('slug' => 'Teasers'), 
    'capability_type' => 'page', 
    'has_archive' => true, 
    'hierarchical' => true, 
    'menu_position' => 60, 
    'supports' => array('title', 'thumbnail', 'page-attributes'), 

); 

    register_post_type('teasers', $args); 
} 
add_action('init', 'teasers_custom_init'); 


//adding the meta box when the admin panel initialises 
add_action("admin_init", "admin_init"); 
// this adds the save teaser function on save post 
add_action('save_post', 'save_teaser'); 

function admin_init(){ 
    add_meta_box('teaser_loop', 'Loop Time', 'loop_meta', 'teasers', 'normal', 'default'); 
} 
// callback function of add meta box that displays the meta box in the post edit screen 
function loop_meta($post, $args){ 

    $teaser_loop = get_post_meta($post->ID, 'teaser_loop', true); 

?> 
    <label>Teaser Loop: </label><input type="text" name="teaser_loop" value="<?php echo $teaser_loop; ?>" /><br/> 

<?php 

} 

// saving the teaser 
function save_teaser(){ 
    global $post; 
    update_post_meta($post->ID, 'teaser_loop', $_POST['teaser_loop']); 
} 

を私は追加のメタボックスを追加したいのですが、何が最善のアプローチですか?

admin_init関数で別のadd_meta_box呼び出しを追加しようとしましたが、このメタボックスhtmlに対して追加のコールバック関数を作成しましたが、フロントエンドで何も生成されませんでした。どのポインタも素晴らしいでしょう。

EDIT:これは(この作品)私は複数のメタボックスのためにそれをやった方法です:

//adding the meta box when the admin panel initialises 
    add_action("admin_init", "admin_init"); 
    // this adds the save teaser function on save post 
    add_action('save_post', 'save_teaser'); 
    function admin_init(){ 
     add_meta_box('teaser_loop', 'Loop Time', 'loop_meta_1', 'teasers', 'normal', 'default'); 
     add_meta_box('teaser_link', 'Teaser Link', 'loop_meta_2', 'teasers', 'normal', 'default'); 
    } 
    // back function of add meta box that displays the meta box in the post edit screen 
    function loop_meta_1($post, $args){ 


     $teaser_loop = get_post_meta($post->ID, 'teaser_loop', true); 

?> 
    <label>Teaser Loop: </label><input type="text" name="teaser_loop" value="<?php echo $teaser_loop; ?>" /><br/> 

<?php 

} 

    function loop_meta_2($post, $args){ 

     $teaser_link = get_post_meta($post->ID, 'teaser_link', true); 

?> 
    <label>Teaser Link: </label><input type="text" name="teaser_link" value="<?php echo $teaser_link; ?>" /><br/> 

<?php 

} 

// saving the teaser 
function save_teaser(){ 
    global $post; 
    update_post_meta($post->ID, 'teaser_loop', $_POST['teaser_loop']); 
    update_post_meta($post->ID, 'teaser_link', $_POST['teaser_link']); 
} 
+0

また、HTTPでこれを求めることができる://wordpress.stackexをchange.com/ – Joren

+0

または[codereview.se]で、最適化を目指す作業コードです。 – brasofilo

+0

私は賞金を出しましたが、ウェブ上のどこにもこの問題に対する動的なアプローチがないためです。私は、すべてのフォーム要素を組み込んだ最適なソリューションが何であるか疑問に思っていました。 – Joren

答えて

3

あなたは、コードを次のようにカスタムポストタイプにmetaboxを追加することができます。 metaboxをレンダリングする関数の名前 -

まず、my_custom_post_typeは、カスタムポストタイプとmy_metabox_renderあなたの名前であることをmetabox

add_action('admin_init', 'my_theme_on_admin_init'); 

function my_theme_on_admin_init() { 
    add_meta_box('my_metabox', 
     __('My metabox', 'textdomain'), 
     'my_metabox_render', 
     'my_custom_post_type', 'normal', 'high' 
    ); 
} 

お知らせを作成します。ユーザーはそれを完全にクラスにカプセル化することができ

add_action('wp_insert_post', 'save_my_meta', 10, 2); 

function save_my_meta($id) { 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $id; 
    if (!current_user_can('edit_posts')) 
     return; 

    if (!isset($id)) 
     $id = (int) $_REQUEST['post_ID']; 

    if (isset($_POST['my_meta_value']) && wp_verify_nonce($_POST['my_meta_value'], 'add_my_meta')) { 
     $data = $_POST['my_meta_value']; 
     if (isset($data)) { 
      update_post_meta($id, '_meta_key', $data); 
     } 
     else { 
      delete_post_meta($id, '_meta_key'); 
     } 
    } 
} 
+0

こんにちはウラジミール。応答していただきありがとうございます。 1つのメタボックスを追加することはできますが、カスタム投稿タイプに複数のメタボックスを追加するにはどうすればよいですか?それが私の持つ問題です。 – BIOS

+0

作成したいメタボックスごとに 'add_meta_box'関数を呼び出します。また、各メタボックスには独自のレンダリング機能が必要です。 – Vladimir

+0

複数のメタボックスを追加しようとしているコードを表示できますか? – Vladimir

7

ポスト保存するときに、あなたがあなたのメタデータを更新する必要以上

レンダリング機能は、すべてのnessesaryフィールド

function my_metabox_render($post) { 
    $data = get_post_meta($post->ID, '_meta_key', true); 
    // Use nonce for verification 
    wp_nonce_field('add_my_meta', 'my_meta_nonce'); 
?> 
<div class="inside"> 
    <table class="form-table"> 
     <tr valign="top"> 
      <th scope="row"><label for="my_meta_value"><?php _e('My meta', 'textdomain'); ?></label></th> 
      <td><textarea id="my_meta_value" name="my_meta_value" cols="40" rows="5"><?php echo (isset($data)) ? $data : ''; ?></textarea></td> 
     </tr> 
    </table> 
</div> 
<?php 
} 

を作成する必要があります。ここでは、私はカスタム投稿タイプの追加を扱っていませんし、テキストとチェックボックスの2つの単純な出力フィールドしかありません。完全な本格的な作業コードは、それぞれの入力タイプを処理する必要があります。

<?php 
/** 
* Plugin Name: Sample Dynamic Meta Boxes 
* Plugin URI: http://stackoverflow.com/q/13903529/1287812 
* Author:  brasofilo 
*/ 
class B5F_Dynamic_Meta_Boxes 
{ 
    private $boxes; 

    # Safe to start up 
    public function __construct($args) 
    { 
     $this->boxes = $args; 
     add_action('plugins_loaded', array($this, 'start_up')); 
    } 

    public function start_up() 
    { 
     add_action('add_meta_boxes', array($this, 'add_mb')); 
    } 

    public function add_mb() 
    { 
     foreach($this->boxes as $box) 
      add_meta_box( 
       $box['id'], 
       $box['title'], 
       array($this, 'mb_callback'), 
       $box['post_type'], 
       isset($box['context']) ? $box['context'] : 'normal', 
       isset($box['priority']) ? $box['priority'] : 'default', 
       $box['args'] 
      ); 
    } 

    # Callback function, uses helper function to print each meta box 
    public function mb_callback($post, $box) 
    { 
     switch($box['args']['field']) 
     { 
      case 'textfield': 
       $this->textfield($box, $post->ID); 
      break; 
      case 'checkbox': 
       $this->checkbox($box, $post->ID); 
      break; 
     } 
    } 

    private function textfield($box, $post_id) 
    { 
     $post_meta = get_post_meta($post_id, $box['id'], true); 
     printf(
      '<label>%s: <input type="text" name="%s" value="%s" /></label> <small>%s</small><br/>', 
      $box['title'], 
      $box['id'], 
      $post_meta, 
      $box['args']['desc'] 
     ); 
    } 

    private function checkbox($box, $post_id) 
    { 
     $post_meta = get_post_meta($post_id, $box['id'], true); 
     printf(
      '<label>%s: </label><input type="checkbox" name="%s" %s /> <small>%s</small><br/>', 
      $box['title'], 
      $box['id'], 
      checked(1, $post_meta, false), 
      $box['args']['desc'] 
     ); 
    } 
} 

# ADD TWO META BOXES - DIFFERENT POST TYPES - DIFFERENT CONTEXTS AND PRIORITIES 
$args = array(
    array(
     'id' => 'teaser_loop', 
     'title' => 'Loop Time', 
     'post_type' => 'post', 
     'args' => array(
      'desc' => 'Enter the time', 
      'field' => 'textfield', 
     ) 
    ), 
    array(
     'id' => 'teaser_link', 
     'title' => 'Loop Link', 
     'post_type' => 'page', 
     'context' => 'side', 
     'priority' => 'high', 
     'args' => array(
      'desc' => 'Open link', 
      'field' => 'checkbox', 
     ) 
    ), 
); 
new B5F_Dynamic_Meta_Boxes($args); 

# ADD ANOTHER META BOX TO ANOTHER POST TYPE 
$more_args = array(
    array(
     'id' => 'extra_box', 
     'title' => 'And another one', 
     'post_type' => 'teaser', 
     'args' => array(
      'desc' => 'Open link', 
      'field' => 'textfield', 
     ) 
    ), 
); 
new B5F_Dynamic_Meta_Boxes($more_args); 

これはちょうどスケルトンであり、ここからたくさん書かれています。いくつかの例:

+0

これはテキスト入力に限られます。これは、ラジオ/チェックボックス、ドロップダウンメニュー、またはその他のフォーム要素で作業している場合は機能しません。 最初の部分は私がthoが好きですが、少し過剰に設計されたようです – Joren

+0

@Joren、まあ、それは単なる概要です。クラスを使用するほうが適切です。微調整はユーザー次第です。奨励金にカスタム説明を入れていない、あなたが期待していることを述べている質問にコメントを投稿できますか? – brasofilo

+0

@brasofilo:あなたの 'class'を使うことは可能ですか?単一のメタボックス内に複数のメタフィールドを追加することができますか? –

関連する問題