Affichage des champs personnalisés

Par Lulu.74, il y a 7 ans


Bonjour j'ai suivit ce tuto : https://www.grafikart.fr/tutoriels/champs-meta-box-787 et je me demandais une fois tout ça terminé comment l'afficher en front (dans page.php par exemple)

Merci d'avance si vous avez la solution :)

6 réponses

Lulu.74, il y a 7 ans

J'ai essayé ceci mais sans succès : (à côté de mon content dans page.php) le type c est un post, mon id de field

get_metadata('post', 'blocs_images', '', false);
Lulu.74, il y a 7 ans

Okay super merci
Je vais regarder
Je savais vraiment pas où regarder :)

shadow49, il y a 7 ans

Si tu regarde bien la doc, le second paramètre doit être un entier et non une string

Lulu.74, il y a 7 ans

Oui mais le truc c'est que j'ai créé un id ici :

<?php class Content_metabox { private $id; private $title; private $post_type; private $fields = []; public static function addJS () { add_action('admin_enqueue_scripts', function (){ wp_register_script('uploaderjs', get_template_directory_uri() . '/assets/js/uploader.js'); wp_enqueue_script('uploaderjs'); }); } public function __construct($id, $title, $post_type){ add_action('admin_init', array(&$this, 'create')); add_action('save_post', array(&$this, 'save')); $this->id = $id; $this->title = $title; $this->post_type = $post_type; } public function create() { if(function_exists('add_meta_box')) { // id - title - name - location add_meta_box($this->id, $this->title, array(&$this, 'render'), $this->post_type); } } public function save($post_id) { // On en fait rien en cas de save Ajax if( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) ) { return false; } // Vérifier les permissions if(!current_user_can('edit_post', $post_id)){ return false; } // On vérifie le nonce if(!wp_verify_nonce($_POST[$this->id . '_nonce'], $this->id)){ return false; } foreach ($this->fields as $field) { $meta = $field['id']; if(isset($_POST[$meta])){ $value = $_POST[$meta]; if(get_post_meta($post_id, $meta)){ update_post_meta($post_id, $meta, $value); } else { if ($value === '') { delete_post_meta($post_id, $meta); } else { add_post_meta($post_id, $meta, $value); } } } } } public function render() { global $post; foreach($this->fields as $field){ extract($field); $value = get_post_meta($post->ID, $id, true); if($value == ''){ $value = $default; } require __DIR__ . '/' . $field['type'] . '.php' ; } echo '<input type="hidden" name="' . $this->id . '_nonce" value="' . wp_create_nonce($this->id) . '">'; } public function add($id, $label, $type = 'text', $default = '') { $this->fields[] = [ 'id' => $id, 'name' => $label, 'type' => $type, 'default' => $default ]; return $this; } } Content_metabox::addJS(); $box = new Content_metabox('blocs_images', 'bloc content', 'page'); $box->add('content_textarea', 'Text : ', 'wysiwyg')->add('content_image', 'Image', 'uploader');
Lulu.74, il y a 7 ans

J'ai fait ceci dans page.php

foreach($custom as $key => $value) { echo $key.': '.$value.'<br />'; }

et ça m'a affiché ceci :

_edit_last: Array
_edit_lock: Array
content_textarea: Array
content_image: Array