2012-12-16 35 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 के लिए अतिरिक्त कॉलबैक फ़ंक्शन भी बनाया लेकिन फ्रंट एंड पर कुछ भी उत्पन्न नहीं हुआ। कोई संकेतक महान होगा।

संपादित करें: तो यह (यह काम करता है) मैं इसे कैसे एक से अधिक मेटा बॉक्स के लिए किया था:

//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 के

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

सूचना बनाते हैं तो वह my_custom_post_type आप के नाम कस्टम प्रकार और my_metabox_render पोस्ट है - एक समारोह है कि Metabox के renders का नाम है। से आप आप मेटाडाटा अद्यतन करना चाहिए जब उपयोगकर्ता पोस्ट

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

हाय व्लादिमीर। जवाब के लिए धन्यवाद। एक मेटा बॉक्स जोड़ना ठीक है लेकिन मैं एक कस्टम पोस्ट प्रकार में एक से अधिक मेटा बॉक्स कैसे जोड़ूं? यही वह मुद्दा है जो मैं कर रहा हूं। – 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 
} 

बनाना चाहिए। यहां, मैं कस्टम पोस्ट प्रकार जोड़ने से काम नहीं कर रहा हूं, और केवल दो साधारण आउटपुट फ़ील्ड्स, टेक्स्ट और चेकबॉक्स हैं। एक पूर्ण कामकाजी कोड प्रत्येक वांछित इनपुट प्रकार का ख्याल रखना चाहिए।

<?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

यह पाठ इनपुट तक ही सीमित है। यदि आप रेडियो/चेक बॉक्स, ड्रॉपडाउन मेनू या अन्य फॉर्म तत्वों के साथ काम कर रहे हैं तो यह काम नहीं करता है। पहला भाग मुझे पसंद है, लेकिन लगता है कि थोड़ा अधिक इंजीनियर – Joren

+0

@ जोरेन, ठीक है, यह सिर्फ एक रूपरेखा है। एक वर्ग का उपयोग करना अधिक उपयुक्त होगा। ठीक-ठीक यह उपयोगकर्ता के लिए है। आपने बक्षीस में कोई कस्टम विवरण नहीं दिया है, क्या आप इस प्रश्न में एक टिप्पणी पोस्ट कर सकते हैं कि आप क्या उम्मीद कर रहे हैं? – brasofilo

+0

@brasofilo: क्या यह आपकी 'कक्षा' का उपयोग कर संभव है, मैं एक मेटा बॉक्स में एकाधिक मेटा फ़ील्ड जोड़ सकता हूं, और वहां टैब कार्यक्षमता भी जोड़ सकता हूं? –