Hinzufügen mehrerer meta-Boxen ein custom post type in wordpress

Ich habe den folgenden code fügt eine benutzerdefinierte post-Typ für die admin-dash und eine benutzerdefinierte meta box für die post-edit-Fenster:

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']);  
}

Meine Frage ist, wenn ich will, um eine zusätzliche meta-box, was ist der beste Ansatz?

Ich habe versucht, das hinzufügen eines weiteren add_meta_box Anruf in admin_init-Funktion und auch eine zusätzliche callback-Funktion für diese meta-box-html aber es wurde nichts generiert, die auf dem front-end. Irgendwelche Hinweise wären Super.

EDIT: So ich habe es für mehr als ein meta-Feld (funktioniert):

//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']);
}
  • Sie könnten auch Fragen Sie diese bei wordpress.stackexchange.com
  • Oder bei Code Überprüfen, wie das funktioniert code Ziel optimiert werden.
  • Ich habe die bounty, weil nirgendwo im web gibt es eine dynamische Herangehensweise an dieses problem. Ich Frage mich, was die optimale Lösung wäre, beinhaltet, dass alle form-Elemente.
  • Genial, das ist genau das, was ich suchte. Danke!
InformationsquelleAutor BIOS | 2012-12-16
Schreibe einen Kommentar