WhatsApp: +39 339 289 627 8
E-mail: info@creazione-siti-web.eu

 

Creare metabox in Wordpress

 

In file functions.php:

function my_theme_add_metabox() { add_meta_box( 'attribut_id_metabox', $title = 'Url img slider', $callback='my_theme_add_metabox_callback', $screen = array('post'), $context = 'advanced', $priority = 'default', $callback_args = null ); } function my_theme_add_metabox_callback($post, $meta) { $screens = $meta['args']; $my_filed_url = get_post_meta($post->ID, 'my_filed_url', true); wp_nonce_field( 'my_nonce_action', 'my_nonce_name' ); break_php <label>Url</label> <input type="text" name="my_filed_url" value="<?php echo $my_filed_url ?>"> open_php } add_action('add_meta_boxes', 'my_theme_add_metabox'); function my_save_postdata( $post_id ) { if ( ! isset( $_POST['my_filed_url'] ) ) return; if ( ! wp_verify_nonce( $_POST['my_nonce_name'], 'my_nonce_action') ) return; if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; if( ! current_user_can( 'edit_post', $post_id ) ) return; $my_data = sanitize_text_field( $_POST['my_filed_url'] ); // Обновляем данные в базе данных. update_post_meta( $post_id, 'my_filed_url', $my_data ); } add_action( 'save_post', 'my_save_postdata' );

Per stampare in loop di template del tema:

echo get_post_meta( get_the_ID(), $key = 'my_filed_url', $single = true )