Advertisement
WalterTosolini

post in series

Jan 13th, 2015
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Post in Serie
  4. Plugin URI: http://www.tosolini.info/2015/01/wordpress-crei…-post-in-serie/
  5. Description: Permette di aggiungere dei post in una serie e mostrare i titoli.
  6. Version: 1.0
  7. Author: Walter Tosolini
  8. Author URI: http://www.tosolini.info
  9. License: GPL2
  10. */
  11.  
  12. // creiamo una tassonomia chiamata "serie" per i post.
  13. function post_seriali() {
  14.     $labels = array(
  15.         'name' => _x('Post in Serie', 'Nome generale della Tassonomia'),
  16.         'singular_name' => _x('Post in Serie', 'Nome singolo della Tassonomia'),
  17.         'all_items' => __('Tutti i Post in Serie'),
  18.         'edit_item' => __('Modifica Post in Serie'),
  19.         'update_item' => __('Aggiorna Post in Serie'),
  20.         'add_new_item' => __('Aggiungi un nuovo Post in Serie'),
  21.         'new_item_name' => __('Nuova Serie di Post'),
  22.         'menu_name' => __('Post Seriali'),
  23.     );
  24.  
  25.     register_taxonomy(
  26.         'serie',
  27.         array('post'), /* Utilizzato per i post, se vuoi aggiungere altro devi usare un array tipo ('post','page','custom-post-type') */
  28.         array(
  29.             'hierarchical' => true, /* true viene utilizzato come categoria, false come tag */
  30.             'labels' => $labels,
  31.             'show_ui' => true, /* mostra interfaccia in backend */
  32.             'show_admin_column' => true,
  33.             'query_var' => true,
  34.             'rewrite' => array('slug' => 'serie'), /* utilizziamo i permalinks per i SEO in modo da trovare i nostri post in serie */
  35.         )
  36.     );
  37. } add_action('init', 'post_seriali', 0);
  38.  
  39. // Costruiamo la Funzione Shortcode
  40. function guida_sc($atts) {
  41.     extract(
  42.         shortcode_atts(
  43.             array(
  44.                 "slug" => '',
  45.                 "id" => '',
  46.                 "titolo" => '',
  47.                 "titolo_element" => 'h3',
  48.                 "titolo_link" => true,
  49.                 "element" => 'ul',
  50.                 "limite" => -1,
  51.                 "future" => true
  52.             ), $atts
  53.         )
  54.     );
  55.     if($id) {
  56.         $tax_query = array(array('taxonomy' => 'serie', 'field' => 'id', 'terms' => $id));
  57.     } else if ($slug) {
  58.         $tax_query = array(array('taxonomy' => 'serie', 'field' => 'slug', 'terms' => $slug));
  59.     } else {
  60.         // Abbiamo cercato "Serie" in id e slug, se non li troviamo utilizziamo la nostra tassonomia
  61.         $terms = get_the_terms($post->ID,'serie');
  62.         if ($terms && !is_wp_error($terms)) {
  63.             foreach ($terms as $term) {
  64.                 $tax_query = array(array('taxonomy' => 'serie', 'field' => 'slug', 'terms' => $term->slug));
  65.             }
  66.         } else {$error = true;}
  67.     }
  68.  
  69.     if($titolo) {
  70.         if($titolo_link == true){
  71.                 $terms = get_terms( 'serie' );
  72.                     foreach ( $terms as $term ) {
  73.                     $term_link = get_term_link( $term );
  74.                     // If there was an error, continue to the next term.
  75.                     if ( is_wp_error( $term_link ) ) {
  76.                         continue;
  77.                     }
  78.                 }      
  79.         $titolo_output = '<'.$titolo_element.' class="post-serie-titolo"><a href="'.esc_url( $term_link ).'">'.$titolo.'</a></'.$titolo_element.'>';           
  80.         } else {
  81.         $titolo_output = '<'.$titolo_element.' class="post-serie-titolo">'.$titolo.'</'.$titolo_element.'>';           
  82.         }
  83.  
  84.     }
  85.     if($future == true) {
  86.         $post_status = array('publish','future');
  87.     } else {
  88.         $post_status = 'publish';
  89.     }
  90.     if($error == false) {
  91.         $args = array(
  92.             'tax_query' => $tax_query,
  93.             'posts_per_page' => $limite,
  94.             'orderby' => 'date',
  95.             'order' => 'ASC',
  96.             'post_status' => $post_status
  97.         );
  98.         $the_posts = get_posts($args);
  99.         /* se esiste un solo post invece di una serie non mostrare la lista */
  100.         if(count($the_posts) > 1) {
  101.             $output = $titolo_output;
  102.             // creo la lista di tag
  103.             $output .= '<'.$element.' class="post-serie-lista">';
  104.             // creo il loop per mostrare la lista di post
  105.             foreach($the_posts as $post) {
  106.                 setup_postdata($post);
  107.                 if($post->post_status == 'publish') {
  108.                     $output .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></li>';
  109.                 } else {
  110.                     /* I post programmati non hanno un link ancora, quindi li mostro come elenco */
  111.                     $output .= '<li>Articoli in programmazione: '.get_the_title($post->ID).'</li>';
  112.                 }
  113.             }
  114.             wp_reset_query();
  115.             // make-up chiudiamo la lista
  116.             $output .= '</'.$element.'>';
  117.             // mostriamo l'intero costrutto
  118.             return $output;
  119.         }
  120.     }
  121. } add_shortcode('guida','guida_sc');
  122.  
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement