Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Post in Serie
- Plugin URI: http://www.tosolini.info/2015/01/wordpress-creiβ¦-post-in-serie/
- Description: Permette di aggiungere dei post in una serie e mostrare i titoli.
- Version: 1.0
- Author: Walter Tosolini
- Author URI: http://www.tosolini.info
- License: GPL2
- */
- // creiamo una tassonomia chiamata "serie" per i post.
- function post_seriali() {
- $labels = array(
- 'name' => _x('Post in Serie', 'Nome generale della Tassonomia'),
- 'singular_name' => _x('Post in Serie', 'Nome singolo della Tassonomia'),
- 'all_items' => __('Tutti i Post in Serie'),
- 'edit_item' => __('Modifica Post in Serie'),
- 'update_item' => __('Aggiorna Post in Serie'),
- 'add_new_item' => __('Aggiungi un nuovo Post in Serie'),
- 'new_item_name' => __('Nuova Serie di Post'),
- 'menu_name' => __('Post Seriali'),
- );
- register_taxonomy(
- 'serie',
- array('post'), /* Utilizzato per i post, se vuoi aggiungere altro devi usare un array tipo ('post','page','custom-post-type') */
- array(
- 'hierarchical' => true, /* true viene utilizzato come categoria, false come tag */
- 'labels' => $labels,
- 'show_ui' => true, /* mostra interfaccia in backend */
- 'show_admin_column' => true,
- 'query_var' => true,
- 'rewrite' => array('slug' => 'serie'), /* utilizziamo i permalinks per i SEO in modo da trovare i nostri post in serie */
- )
- );
- } add_action('init', 'post_seriali', 0);
- // Costruiamo la Funzione Shortcode
- function guida_sc($atts) {
- extract(
- shortcode_atts(
- array(
- "slug" => '',
- "id" => '',
- "titolo" => '',
- "titolo_element" => 'h3',
- "titolo_link" => true,
- "element" => 'ul',
- "limite" => -1,
- "future" => true
- ), $atts
- )
- );
- if($id) {
- $tax_query = array(array('taxonomy' => 'serie', 'field' => 'id', 'terms' => $id));
- } else if ($slug) {
- $tax_query = array(array('taxonomy' => 'serie', 'field' => 'slug', 'terms' => $slug));
- } else {
- // Abbiamo cercato "Serie" in id e slug, se non li troviamo utilizziamo la nostra tassonomia
- $terms = get_the_terms($post->ID,'serie');
- if ($terms && !is_wp_error($terms)) {
- foreach ($terms as $term) {
- $tax_query = array(array('taxonomy' => 'serie', 'field' => 'slug', 'terms' => $term->slug));
- }
- } else {$error = true;}
- }
- if($titolo) {
- if($titolo_link == true){
- $terms = get_terms( 'serie' );
- foreach ( $terms as $term ) {
- $term_link = get_term_link( $term );
- // If there was an error, continue to the next term.
- if ( is_wp_error( $term_link ) ) {
- continue;
- }
- }
- $titolo_output = '<'.$titolo_element.' class="post-serie-titolo"><a href="'.esc_url( $term_link ).'">'.$titolo.'</a></'.$titolo_element.'>';
- } else {
- $titolo_output = '<'.$titolo_element.' class="post-serie-titolo">'.$titolo.'</'.$titolo_element.'>';
- }
- }
- if($future == true) {
- $post_status = array('publish','future');
- } else {
- $post_status = 'publish';
- }
- if($error == false) {
- $args = array(
- 'tax_query' => $tax_query,
- 'posts_per_page' => $limite,
- 'orderby' => 'date',
- 'order' => 'ASC',
- 'post_status' => $post_status
- );
- $the_posts = get_posts($args);
- /* se esiste un solo post invece di una serie non mostrare la lista */
- if(count($the_posts) > 1) {
- $output = $titolo_output;
- // creo la lista di tag
- $output .= '<'.$element.' class="post-serie-lista">';
- // creo il loop per mostrare la lista di post
- foreach($the_posts as $post) {
- setup_postdata($post);
- if($post->post_status == 'publish') {
- $output .= '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a></li>';
- } else {
- /* I post programmati non hanno un link ancora, quindi li mostro come elenco */
- $output .= '<li>Articoli in programmazione: '.get_the_title($post->ID).'</li>';
- }
- }
- wp_reset_query();
- // make-up chiudiamo la lista
- $output .= '</'.$element.'>';
- // mostriamo l'intero costrutto
- return $output;
- }
- }
- } add_shortcode('guida','guida_sc');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement