Úvodní stránka › Fórum podpory WordPressu › Šablony, CSS (zobrazení webu) › Více cutom post type s custom meta boxama
Štítky: custom meta box
Zvolené téma neobsahuje zatím žádnou odpověď. Do diskuze (1 účastník) se naposledy zapojil uživatel Nik a poslední změna je stará 8 let, 2 měsíce.
-
AutorPříspěvky
-
7. února 2016 (20:22) #26439
Ahoj,
poprvé pracuji se custom post type a hned ji potřebuji mít na stránce víc. To by zatím nebyl problém.
Problém je v tom, že se mi nedaří uložit data z custom meta boxu.Můj kód:
<?php // SET CUSTOM POST TYPE function create_posttype() { // modul Kmeny register_post_type('kmen-cpt', array( 'labels' => array( 'name' => __('Kmeny'), //name in menu 'singular_name' => __('Kmen'), 'add_new' => __('Přidat nový kmen'), //word in menu 'add_new_item' => __('Nový kmen'), // editor headline when is new 'edit_item' => __('Upravit kmen'), //editor headline when is editing 'new_item' => __('Nový kmen'), 'view_item' => __('Zobrazit kmen'), //view button in editor 'search_items' => __('Hledat kmen'), 'not_found' => __('Žádné kmeny nebyly nalezeny'), 'not_found_in_trash' => __('Žádné kmeny nebyly nalezeny v koši') ), 'menu_position' => 2, 'public' => true, 'supports' => array('title', 'editor', 'thumbnail', 'revisions'), 'has_archive' => true, 'rewrite' => array('slug' => 'kmen'), ) ); // modul Odkazy register_post_type('odkazy-cpt', array( 'labels' => array( 'name' => __('Odkazy'), //name in menu 'singular_name' => __('Odkaz'), 'add_new' => __('Přidat nový Odkaz'), //word in menu 'add_new_item' => __('Nový odkaz'), // editor headline when is new 'edit_item' => __('Upravit odkaz'), //editor headline when is editing 'new_item' => __('Nový odkaz'), 'view_item' => __('Zobrazit odkaz'), //view button in editor 'search_items' => __('Hledat odkaz'), 'not_found' => __('Žádné odkazy nebyly nalezeny'), 'not_found_in_trash' => __('Žádné odkazy nebyly nalezeny v koši') ), 'menu_position' => 3, 'public' => true, 'supports' => array('title', 'editor', 'thumbnail', 'revisions'), 'has_archive' => true, 'rewrite' => array('slug' => 'kmen'), ) ); } add_action('init', 'create_posttype'); // SET CUSTOM META BOXES $prefix = array('kmen_', 'odkaz_'); $meta_box = array( // modul Kmeny 'kmen-cpt' => array( 'id' => 'kmen-meta', 'title' => 'Další informace', 'page' => 'kmen-cpt', 'context' => 'normal', 'priority' => 'high', 'fields' => array( array( 'name' => 'Slogan:', 'desc' => '', 'id' => $prefix[0] . 'desc', 'type' => 'textarea', 'std' => '' ), array( 'name' => 'Jméno vedoucího:', 'desc' => '', 'id' => $prefix[0] . 'manager', 'type' => 'text', 'std' => '' ), array( 'name' => 'E-mail na vedoucího:', 'desc' => '', 'id' => $prefix[0] . 'mail', 'type' => 'text', 'std' => '' ), array( 'name' => 'Webové stránky:', 'desc' => '', 'id' => $prefix[0] . 'web', 'type' => 'text', 'std' => '' ), array( 'name' => 'Facebook:', 'desc' => '', 'id' => $prefix[0] . 'fb', 'type' => 'text', 'std' => '' ), array( 'name' => 'Středisko:', 'desc' => '', 'id' => $prefix[0] . 'unit', 'type' => 'text', 'std' => '' ) ) ), // modul Odkazy 'odkazy-cpt' => array( 'id' => 'odkaz-meta', 'title' => 'Další informace', 'page' => 'odkazy-cpt', 'context' => 'side', 'priority' => 'high', 'fields' => array( array( 'name' => 'Adresa:', 'desc' => '', 'id' => $prefix[1] . 'url', 'type' => 'text', 'std' => '' ), ) ) ); // ADD CUSTOM META BOXES function mytheme_add_box() { global $meta_box; foreach ($meta_box as $item) { add_meta_box( $item['id'], $item['title'], 'mytheme_show_box', $item['page'], $item['context'], $item['priority'], $item ); } } add_action('admin_menu', 'mytheme_add_box'); // DISPLAY CUSTOM META BOXES function mytheme_show_box($post, $item) { // Use nonce for verification echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; echo '<table class="form-table">'; foreach ($item['args']['fields'] as $field) { // get current post meta data $meta = get_post_meta($post->ID, $field['id'], true); echo '<tr>', '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>', '<td>'; // display right element switch ($field['type']) { case 'text': echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc']; break; case 'textarea': echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc']; break; case 'select': echo '<select name="', $field['id'], '" id="', $field['id'], '">'; foreach ($field['options'] as $option) { echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>'; } echo '</select>'; break; case 'radio': foreach ($field['options'] as $option) { echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name']; } break; case 'checkbox': echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />'; break; } echo '</td><td>', '</td></tr>'; } echo '</table>'; } //add_action('save_post', 'mytheme_save_data'); // Save the Metabox Data- function wpt_save_events_meta($post_id, $post) { // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if (!wp_verify_nonce($_POST['eventmeta_noncename'], plugin_basename(__FILE__))) { return $post->ID; } // Is the user allowed to edit the post or page? if (!current_user_can('edit_post', $post->ID)) return $post->ID; // OK, we're authenticated: we need to find and save the data // We'll put it into an array to make it easier to loop though. $events_meta['_location'] = $_POST['_location']; // Add values of $events_meta as custom fields foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array! if ($post->post_type == 'revision') return; // Don't store custom data twice $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) if (get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value update_post_meta($post->ID, $key, $value); } else { // If the custom field doesn't have a value add_post_meta($post->ID, $key, $value); } if (!$value) delete_post_meta($post->ID, $key); // Delete if blank } } add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
Dělal jsem to podle návodu pro jeden custom post type a upravoval to tak, aby to fungovalo i na dva. Poslední část od komentáře “Save the metabox data” je jen zkopírovaná. Něco jsem zkoušel, ale bez valného výsledku. Moc nevím, jak ukládání funguje a jak to udělat, aby to ukládalo data jen z patřičných metaboxů.
Díky z akaždou radu.
-
AutorPříspěvky
Pokud chcete odpovědět na toto téma, musíte se nejdříve přihlásit.