Last active
September 29, 2017 07:25
-
-
Save bluvertigo/df68751210261dd54e25 to your computer and use it in GitHub Desktop.
Shortcode - Restituisce la lista degli ultimi articoli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Shortcode per visualizzare gli ultimi post | |
| add_shortcode( 'list-posts', 'list_posts_shortcode' ); | |
| function list_posts_shortcode( $atts ) { | |
| ob_start(); | |
| // define attributes and their defaults | |
| extract( shortcode_atts( array ( | |
| 'type' => 'post', | |
| 'order' => 'ASC', | |
| 'orderby' => 'date', | |
| 'posts_per_page' => -1 | |
| ), $atts ) ); | |
| // define query parameters based on attributes | |
| $options = array( | |
| 'post_type' => $type, | |
| 'order' => $order, | |
| 'orderby' => $orderby, | |
| 'posts_per_page' => $posts_per_page | |
| ); | |
| $q = new WP_Query( $options ); | |
| $list = '<ul class="recent-posts">'; | |
| while($q->have_posts()) : $q->the_post(); | |
| $list .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '<br />' . get_the_excerpt() . '</li>'; | |
| endwhile; | |
| wp_reset_query(); | |
| return $list . '</ul>'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment