Function latest post in wordpress -
i have next code show latest post in wordpress. want add thumbnail , date. suggestion?
function last_article($atts){ extract(shortcode_atts(array( 'posts' => 1, ), $atts)); $return_string = '<ul>'; query_posts(array('orderby' => 'date', 'order' => 'desc' , 'showposts' => $posts)); if (have_posts()) : while (have_posts()) : the_post(); $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; endif; $return_string .= '</ul>'; wp_reset_query(); return $return_string; }
your shortcode pure evil. should never ever use extract()
. extract()
removed wordpress core. should tell how bad extract()
is. see corresponding trac ticket here
also, never ever use query_posts
.
note: function isn't meant used plugins or themes. explained later, there better, more performant options alter main query. query_posts() overly simplistic , problematic way modify main query of page replacing new instance of query. inefficient (re-runs sql queries) , outright fail in circumstances (especially when dealing posts pagination).
you should using wp_query
you should visit shortcode api in codex , see how construct shortcode
with in mind, code should this
function last_article($atts){ $a = shortcode_atts( array( 'posts' => 1, ), $atts ); $return_string = '<ul>'; $q = new wp_query(array('orderby' => 'date', 'order' => 'desc' , 'posts_per_page' => $a['posts'])); if ($q->have_posts()) { while ($q->have_posts()) { $q->the_post(); $return_string .= '<li>'.the_date().'</li>'; $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; if (has_post_thumbnail()) { $return_string .= '<li>'.the_post_thumbnail().'</li>'; } } } $return_string .= '</ul>'; wp_reset_postdata(); return $return_string; }
Comments
Post a Comment