Advertisement
pskli

Tutoriel WordPress Cuztom #3 par Pierre Saikali

Mar 15th, 2013
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  *  Définition des types de contenus, taxonomies, metaboxes et custom fields
  5.  */
  6.  
  7. // Type de contenu Livre
  8.  
  9. $book = register_cuztom_post_type(
  10.     'Livre',
  11.     array(
  12.         'menu_icon' => get_template_directory_uri() . '/img/book.png'
  13.     )
  14. );
  15.  
  16. $book->add_taxonomy('Genre');
  17.  
  18. // Ajouter une metabox et plusieurs champs
  19.  
  20. $book->add_meta_box(
  21.     'book_info',
  22.     'Information sur ce livre',
  23.     array(
  24.         array(
  25.             'name' => 'publication_date',
  26.             'label' => 'Date de publication',
  27.             'type' => 'date',
  28.             'description' => 'Description qui va sous le titre',
  29.             'explanation' => 'Explication qui va sous le champs',
  30.         ),
  31.         array(
  32.             'name' => 'cover',
  33.             'label' => 'Couverture du livre',
  34.             'type' => 'image',
  35.             'description' => 'Envoyez une image de couverture du livre',
  36.         ),
  37.         array(
  38.             'type' => 'post_select',
  39.             'name' => 'author',
  40.             'label' => 'Auteur du livre',
  41.             'args' => array(
  42.                 'post_type' => 'auteur',
  43.                 'posts_per_page' => -1
  44.             )
  45.         )
  46.     )
  47. );
  48.  
  49. // Type de contenu Auteur
  50.  
  51. $author = register_cuztom_post_type(
  52.     'Auteur',
  53.     array(
  54.         'menu_icon' => get_template_directory_uri() . '/img/pencil.png'
  55.     )
  56. );
  57.  
  58.  
  59.  
  60. /*
  61.  *  Modification des colonnes d'administration des livres
  62.  */
  63.  
  64. function custom_livre_columns_title($columns) {
  65.     $columns['publication_date'] = 'Date de pub.';
  66.     $columns['title'] = 'Titre du livre';
  67.     $columns['date'] = 'Article publié le';
  68.     $columns['genre'] = 'Genres';
  69.  
  70.     $columns = array_slice($columns, 0, 1, true) + array('livre_cover' => 'Couverture de livre') + array_slice($columns, 1, count($columns) - 1, true);
  71.  
  72.     return $columns;
  73. }
  74. add_filter('manage_edit-livre_columns', 'custom_livre_columns_title');
  75.  
  76. function custom_livre_columns_content($column) {
  77.     global $post;
  78.  
  79.     switch ($column) {
  80.         case 'livre_cover':
  81.             $image_id = get_post_meta($post->ID, '_book_info_cover', true);
  82.             $image = wp_get_attachment_image_src($image_id, 'full');
  83.             $image_src = $image[0];
  84.  
  85.             if (isset($image_id) && $image_id != '') { ?>
  86.                 <img style="width:75px;height:auto;" src="<?php echo esc_url($image_src); ?>" alt="<?php echo esc_attr(get_the_title($post->ID)); ?>" />
  87.             <?php }
  88.             break;
  89.  
  90.         case 'publication_date':
  91.             echo get_post_meta($post->ID, '_book_info_publication_date', true);
  92.             break;
  93.  
  94.         case 'genre' :
  95.             echo get_the_term_list($post->ID, 'genre', '', ', ', '');
  96.             break;
  97.     }
  98. }
  99. add_action('manage_livre_posts_custom_column', 'custom_livre_columns_content');
  100.  
  101.  
  102. /*
  103.  *  Modification des colonnes d'administration des auteurs
  104.  */
  105.  
  106. function custom_auteur_columns_title($columns) {
  107.     $columns['number_books'] = 'Nombre de livres';
  108.     return $columns;
  109. }
  110. add_filter('manage_edit-auteur_columns', 'custom_auteur_columns_title');
  111.  
  112. function custom_auteur_columns_content($column) {
  113.     global $post;
  114.  
  115.     switch ($column) {
  116.         case 'number_books':
  117.  
  118.             $args = array(
  119.                 'post_type' => 'livre',
  120.                 'numberposts' => -1,
  121.                 'meta_query' => array(
  122.                     array(
  123.                         'key' => '_book_info_author',
  124.                         'value' => $post->ID
  125.                     )
  126.                 )
  127.             );
  128.  
  129.             $books = get_posts($args);
  130.  
  131.             echo count($books);
  132.  
  133.             break;
  134.     }
  135. }
  136. add_action('manage_auteur_posts_custom_column', 'custom_auteur_columns_content');
  137.  
  138.  
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement