0

I'm using this code to show recent posts with a shortcode that I found at smashingmagazine site. It's not working the correct way, I mean when I specify the number of posts to show, it just shows one post with every number I specify.

Here's the code:

function recent_posts_function() {
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
  if (have_posts()) :
    while (have_posts()) : the_post();
       $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
    endwhile;
  endif;
wp_reset_query();
return $return_string;
}

function register_shortcodes(){
   add_shortcode('recent-posts', 'recent_posts_function');
}

add_action( 'init', 'register_shortcodes');

I've changed the showposts number, but nothing happens. What's wrong?

Any suggestions?

2
  • $return_string is over written every time that you iterate the loop, so it's only going to grab the result from the last row due to this. Change it to $return_string .= to concatenate the string continually, producing a long list of links.
    – Ohgodwhy
    Commented Aug 28, 2014 at 3:26
  • Ok, I fix that part but now the numbers of posts are still wrong, when I put 'showposts' => 1, it shows 6 posts when I put 2 it shows 7 posts and when I put 3 it shows 8 posts!!! what's going on here?
    – Komeyl94
    Commented Aug 28, 2014 at 3:49

1 Answer 1

0

Just before I start, never use query_posts to construct or modify any type of query. It outright fails in many scenarios, specially pagination, and breaks the main query, which you should never do.

If you need to construct custom queries, rather use WP_Query

Also, showposts have been deprerciated long time ago and have been replaced with posts_per_page

You should read the Shortcode API, this should give you an overview of what is happening and how shortcodes should be used and created. One important thing to remember here, shortcode content should be returned, not echo'ed. Here is also a tutorial that help me a lot.

Just a quick tip here, shortcodes should always go into a plugin. If you haven't yet created one, go and read MU-Plugin (must-use-plugin)

The correct way of constructing your shortcode would be as follows: (Your shortcode will be [my-shortcode]) This is untested though

add_shortcode( 'my-shortcode', 'my_custom_query_shortcode' );
function my_custom_query_shortcode( $atts ) {
    ob_start();

$query = new WP_Query(array('orderby' => 'date', 'order' => 'DESC' , 'posts_per_page' => 1));

 if ( $query->have_posts() ) : 
   while($query->have_posts()) : $query->the_post();

        //YOUR LOOP ELEMENTS

      <?php 
      endwhile;

     $myvariable = ob_get_clean();

     return $myvariable;

 endif;    

}

Just replace your code with mine, and make sure that you add your loop elements.

Just one more thing, remember, if you run any custom query on a page, and you have not reset that query, it will influence any other query on that page, even this shortcode, so always make sure that you reset all custom queries once you've done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.