'Custom Posts', 'singular_label' =>'Custom Post', 'public' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'supports' => array('title', 'editor', 'thumbnail' ) )); /********** pour les checkbox à choix multiples : le plus simple est de déclarer une taxonomie hiérarchique (= catégorie) ****************/ register_taxonomy( 'couleurs', $myposttype, array( 'hierarchical' => true, 'label' => 'couleurs', 'query_var' => true, 'rewrite' => true ) ); } $myCustomTypeOptions = array ( array( 'name' => 'Mon champ text', 'desc' => 'Description 1', 'id' => $myposttype.'_champtexte', 'type' => 'text', 'std' => ''), array( 'name' => 'Mon textarea', 'desc' => 'Description 2', 'id' => $myposttype.'_textarea', 'type' => 'textarea', 'std' => 'Youpi'), array( 'name' => 'Mon select', 'desc' => '', 'id' => $myposttype.'_choix', 'type' => 'select', 'options' => array('choix 1','choix 2','choix 3'), 'std' => 'choix 2'), array( 'name' => 'Mon checkbox', 'desc' => 'Case à cocher', 'id' => $myposttype.'_case', 'type' => 'checkbox', 'std' => '1') ); /************** à la fin du formulaire, ajout des champs définis dans $myCustomTypeOptions ***********/ add_action('edit_form_advanced', 'moncustomposttype_form'); add_action('save_post', 'moncustomposttype_save'); function moncustomposttype_form(){ global $myposttype; global $myCustomTypeOptions; if((isset($_GET['post_type'])) and ($_GET['post_type']==$myposttype)) /* formulaire d'ajout */ { echo '
'; } else{ if(!isset($_GET['post_type'])) { if(isset($_GET['post'])) { if(get_post_type($_GET['post'])==$myposttype) /* formulaire de modification */ { $id=$_GET['post']; /* formulaire prérempli avec les valeurs pré-existantes */ echo ''; } } } } } function get_champ($o,$val) { switch ($o['type']) { case 'textarea': echo ' '; break; case 'text': echo ' '; break; case 'checkbox': echo ''; break; case 'select': echo ' '; break; } } /***************** Lors de la sauvegarde, ajout/modification d'un custom field par champs de $myCustomTypeOptions ***********/ function moncustomposttype_save(){ global $myposttype; global $myCustomTypeOptions; if(isset($_POST['post_ID'])) { $id=$_REQUEST['post_ID']; if(get_post_type($id)==$myposttype) /* si on édite bien un post du type $myposttype */ { foreach ($myCustomTypeOptions as $o) { if(isset($_POST[$o['id']])) { update_post_meta($id, $o['id'], $_POST[$o['id']]); } elseif($o['type']=='checkbox') { update_post_meta($id, $o['id'], 0); } } } } } ?>