Custom Post Type and Taxonomies


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // REALISATIONS
  2. add_action('init', 'ajoute_une_realisation');
  3. function ajoute_une_realisation(){
  4. $labels = array(
  5. 'name' => _x('Ajouter une réalisation', 'taxonomy general name'),
  6. 'singular_name' => _x('Ajouter une réalisation', 'taxonomy singular name'),
  7. 'add_new' => _x('Ajouter une réalisation', 'realisation'),
  8. 'add_new_item' => __('Ajouter une réalisation'),
  9. 'edit_item' => __('Modifier une réalisation'),
  10. 'new_item' => __('Ajouter une réalisation'),
  11. 'view_item' => __('Voir une réalisation'),
  12. 'search_items' => __('Rechercher une réalisation'),
  13. 'not_found' => __('Aucune réalisation trouvée'),
  14. 'not_found_in_trash' => __('Aucune réalisation trouvée'),
  15. 'parent_item_colon' => '',
  16. 'menu_name' => 'Réalisations'
  17. );
  18. $args = array(
  19. 'labels' => $labels,
  20. 'public' => true,
  21. 'publicly_queryable' => true,
  22. 'show_ui' => true,
  23. 'show_in_menu' => true,
  24. 'menu_position' => 9,
  25. 'query_var' => true,
  26. //'rewrite' => true,
  27. 'rewrite' => array( 'slug' => 'realisations-et-clients' ),
  28. 'capability_type' => 'page',
  29. 'has_archive' => false,
  30. 'hierarchical' => false,
  31. //'taxonomies' => array('category'),
  32. 'supports' => array('title','editor')
  33. );
  34. register_post_type('realisations',$args);
  35.  
  36. }
  37.  
  38. add_filter("manage_edit-realisations_columns", "realisations_edit_columns");
  39. add_action("manage_posts_custom_column", "realisations_custom_columns");
  40. add_filter("manage_edit-realisations_sortable_columns", "realisations_register_sortable" );
  41.  
  42. function realisations_edit_columns($columns){
  43. $columns = array(
  44. "cb" => "<input type=\"checkbox\" />",
  45. "activer_projet_sur_site" => "Actif sur le site",
  46. "est_un_projet_vedette" => "Projet vedette",
  47. "est_un_projet_branding" => "Projets Branding",
  48. "title" => "Titre du projet",
  49. "icl_translations" => $columns['icl_translations'],
  50. "date" => "Date"
  51. );
  52.  
  53. return $columns;
  54. }
  55.  
  56. function realisations_custom_columns($column){
  57. global $post;
  58. switch ($column)
  59. {
  60. case "activer_projet_sur_site":
  61. $custom = get_post_custom();
  62. if(get_field('acf_activer_le_projet_sur_le_site'))
  63. {
  64. echo 'Oui';
  65. }
  66. else
  67. {
  68. echo 'Non';
  69. }
  70. break;
  71.  
  72. case "est_un_projet_vedette":
  73. $custom = get_post_custom();
  74. if(get_field('acf_real_projet_en_vedette'))
  75. {
  76. echo 'Oui';
  77. }
  78. else
  79. {
  80. echo 'Non';
  81. }
  82. break;
  83. case "est_un_projet_branding":
  84. $custom = get_post_custom();
  85. if(get_field('acf_est_un_projet_branding'))
  86. {
  87. echo 'Oui';
  88. }
  89. else
  90. {
  91. echo 'Non';
  92. }
  93. break;
  94. }
  95. }
  96.  
  97.  
  98.  
  99. function realisations_register_sortable($columns)
  100. {
  101. $columns['activer_projet_sur_site'] = 'activer_projet_sur_site';
  102. $columns['est_un_projet_vedette'] = 'est_un_projet_vedette';
  103. $columns['est_un_projet_branding'] = 'est_un_projet_branding';
  104. return $columns;
  105. }
  106.  
  107.  
  108.  
  109. //hook into the init action and call create_book_taxonomies when it fires
  110. add_action( 'init', 'create_realisations_taxonomies', 0 );
  111.  
  112. //create two taxonomies, genres and writers for the post type "book"
  113. function create_realisations_taxonomies()
  114. {
  115. // Add new taxonomy
  116. $labels = array(
  117. 'name' => _x( 'Type de r&eacute;alisations', 'taxonomy general name' ),
  118. 'singular_name' => _x( 'Type de r&eacute;alisation', 'taxonomy singular name' ),
  119. 'search_items' => __( 'Rechercher un type' ),
  120. 'all_items' => __( 'Tous les types' ),
  121. 'parent_item' => __( 'Parent Types' ),
  122. 'parent_item_colon' => __( 'Parent Types:' ),
  123. 'edit_item' => __( 'Modifier un type' ),
  124. 'update_item' => __( 'Mise &agrave; jour du type' ),
  125. 'add_new_item' => __( 'Ajouter un type' ),
  126. 'new_item_name' => __( 'Nouveau nom du type' ),
  127. 'menu_name' => __( 'Type de r&eacute;alisations' ),
  128. );
  129.  
  130. register_taxonomy('type_realisation',array('realisations'), array(
  131. 'hierarchical' => true,
  132. 'labels' => $labels,
  133. 'show_ui' => true,
  134. 'query_var' => true
  135. ));
  136.  
  137. }
  138.  
  139. /*
  140.  * Example code showing how to hook WordPress to add fields to the taxonomny term edit screen.
  141.  *
  142.  * This example is meant to show how, not to be a drop in example.
  143.  *
  144.  * This example was written in response to this question:
  145.  *
  146.  * http://lists.automattic.com/pipermail/wp-hackers/2010-August/033671.html
  147.  *
  148.  * By:
  149.  *
  150.  * Mike Schinkel (http://mikeschinkel.com/custom-wordpress-plugins/)
  151.  *
  152.  * NOTE:
  153.  *
  154.  * This could easily become a plugin if it were fleshed out.
  155.  * A class with static methods was used to minimize the variables & functions added to the global namespace.
  156.  * wp_options was uses with one option be tax/term instead of via a serialize array because it aids in retrival
  157.  * if there get to be a large number of tax/terms types. A taxonomy/term meta would be the prefered but WordPress
  158.  * does not have one.
  159.  *
  160.  * This example is licensed GPLv2.
  161.  *
  162.  */
  163. /*
  164.  
  165. // These are helper functions you can use elsewhere to access this info
  166. function get_taxonomy_term_type($taxonomy,$term_id) {
  167.   return get_option("_term_type_{$taxonomy}_{$term->term_id}");
  168. }
  169. function update_taxonomy_term_type($taxonomy,$term_id,$value) {
  170.   update_option("_term_type_{$taxonomy}_{$term_id}",$value);
  171. }
  172.  
  173. //This initializes the class.
  174. TaxonomyTermTypes::on_load();
  175.  
  176. //This should be called in your own code. This example uses two taxonomies: 'region' & 'opportunity'
  177. TaxonomyTermTypes::register_taxonomy(array('type_realisation'));
  178.  
  179. class TaxonomyTermTypes {
  180.   //This initializes the hooks to allow saving of the
  181.   static function on_load() {
  182.   add_action('created_term',array(__CLASS__,'term_type_update'),10,3);
  183.   add_action('edit_term',array(__CLASS__,'term_type_update'),10,3);
  184.   }
  185.   //This initializes the hooks to allow adding the dropdown to the form fields
  186.   static function register_taxonomy($taxonomy) {
  187.   if (!is_array($taxonomy))
  188.   $taxonomy = array($taxonomy);
  189.   foreach($taxonomy as $tax_name) {
  190.   add_action("{$tax_name}_add_form_fields",array(__CLASS__,"add_form_fields"));
  191.   add_action("{$tax_name}_edit_form_fields",array(__CLASS__,"edit_form_fields"),10,2);
  192.   }
  193.   }
  194.   // This displays the selections. Edit it to retrieve
  195.   static function add_form_fields($taxonomy) {
  196.   echo "Services associ&eacute;s " . self::get_select_html('strategie');
  197.   }
  198.   // This displays the selections. Edit it to retrieve your own terms however you retrieve them.
  199.   static function get_select_html($selected) {
  200.   $selected_attr = array('strategie'=>'','developpement-web'=>'');
  201.   $selected_attr[$selected] = ' selected="selected"';
  202.   $html =<<<HTML
  203. <select id="tag-type" name="tag-type">
  204.   <option value="strategie"{$selected_attr['strategie']}>Strat&eacute;gie</option>
  205.   <option value="developpement-web"{$selected_attr['developpement-web']}>D&eacute;veloppement Web</option>
  206. </select>
  207. HTML;
  208.   return $html;
  209.   }
  210.   // This a table row with the drop down for an edit screen
  211.   static function edit_form_fields($term, $taxonomy) {
  212.   $selected = get_option("_term_type_{$taxonomy}_{$term->term_id}");
  213.   $select = self::get_select_html($selected);
  214.   $html =<<<HTML
  215. <tr class="form-field form-required">
  216.   <th scope="row" valign="top"><label for="tag-type">Services associ&eacute;s </label></th>
  217.   <td>$select</td>
  218. </tr>
  219. HTML;
  220.   echo $html;
  221.   }
  222.   // These hooks are called after adding and editing to save $_POST['tag-term']
  223.   static function term_type_update($term_id, $tt_id, $taxonomy) {
  224. $varService = $_POST['tag-type'];
  225. if (isset($varService)) {
  226.   update_taxonomy_term_type($taxonomy,$term_id,$varService);
  227.  
  228.   }
  229.   }
  230. }
  231. */
  232. add_filter("manage_edit-type_realisation_columns", 'type_realisation_columns');
  233. //add_action("manage_posts_custom_column", "type_realisation_custom_columns");
  234.  
  235. function type_realisation_columns($type_realisation_columns) {
  236. $new_columns = array(
  237. 'cb' => '<input type="checkbox" />',
  238. 'name' => __('Name'),
  239. 'description' => "Services associ&eacute;s", /* Solution broche � foin en attendant de trouver autre chose */
  240. 'slug' => __('Slug'),
  241. 'posts' => __('Posts')
  242. );
  243. return $new_columns;
  244. }
  245.  
  246. /*
  247. function type_realisation_custom_columns($new_columns){
  248. global $post;
  249. switch ($new_columns)
  250. {
  251. case "services_associes":
  252.   //$new_columns = get_post_custom();
  253. echo("test");
  254. break;
  255. }
  256.  
  257. }
  258. */
  259. /*
  260. add_filter("manage_edit-type_realisation_columns", "type_realisation_edit_columns");
  261. add_action("manage_posts_custom_column", "type_realisation_custom_columns");
  262.  
  263. function type_realisation_edit_columns($columns){
  264. $columns = array(
  265. "cb" => "<input type=\"checkbox\" />",
  266. 'name' => __('Name'),
  267. 'slug' => __('Slug'),
  268.   'posts' => __('Posts'),
  269. "services_associes" => "Services associ&eacute;s"
  270. );
  271.  
  272. return $columns;
  273. }
  274.  
  275. function type_realisation_custom_columns($column){
  276. global $Post;
  277.  
  278. switch ($column) {
  279. case "services_associes":
  280.   $custom = get_post_custom();
  281. echo "test";
  282. break;
  283. }
  284.  
  285. }
  286.  
  287. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.