Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*WordPress Shortcode to display the latest 5 posts
- * PLEASE NOTE: This may not work on certain themes or for everyone!
- * Add the following code to your functions.php then add the shortcode [recent-posts] to the text widget
- * box in the WP Dashboard widgets section. You may need to make changes to your stylesheet's .textwidget or .widget_text classes respectively.
- * Want to display more posts? Change posts_per_page to the number you wish
- * Want to display Pages? Change post_type to 'pages'
- */
- function my_recent_posts($atts){
- $query = new WP_Query( array( 'post_type' =>'post','orderby' => 'date', 'posts_per_page' => '5'));
- $list = '<ul class="recent-posts">';
- while($query->have_posts()) : $query->the_post();
- $list .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '<br />' . get_the_excerpt() . '</li>';
- endwhile;
- wp_reset_postdata();
- return $list . '</ul>';
- }
- add_filter('widget_text', 'do_shortcode');/*Needed to display shortcode in widget*/
- add_shortcode('recent-posts', 'my_recent_posts');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement