0
function blog_post_home_shortcode() {
  $query = new WP_Query( array(
        'post_type' => 'post',
        'order' => 'DESC',
        'post_status' => ' publish',
        'posts_per_page' => 3
    ));

  while ($query->have_posts()): $query->the_post();
      $blog = get_the_title();
  return $blog;
  endwhile;
  wp_reset_postdata();
}
add_shortcode('blogs_home', 'blog_post_home_shortcode');

returns only one title while in WP_Query I have set posts_per_page => 3 I want to display all 3 title of last published posts.

  • I don't understand the question. Setting posts_per_page to 3 should show 3 posts. If -1 (unlimited) is only showing 1, then you only have 1 published post, or another plugin is interfering with your query. – Jacob Peattie Nov 20 '17 at 10:01
  • I have 13 posts Published but only one shows even when I put posts_per_page => -1 – Dharit Soni Nov 20 '17 at 10:02
  • 2
    Why do you have return statement inside while loop? – bravokeyl Nov 20 '17 at 10:05
  • I am creating a shortcode using functions.php so that's why I have to return – Dharit Soni Nov 20 '17 at 10:08
  • You're returning inside the loop, so it's only ever going to output one post. – Jacob Peattie Nov 20 '17 at 10:10
-2

You may please update your code as follows

   function blog_post_home_shortcode() {
  $query = new WP_Query( array(
        'post_type' => 'post',
        'order' => 'DESC',
        'post_status' => ' publish',
        'posts_per_page' => 3
    ));

  while ($query->have_posts()): $query->the_post();
      $blog = get_the_title();
  echo $blog;
  endwhile;
  wp_reset_postdata();
}
add_shortcode('blogs_home', 'blog_post_home_shortcode');

Please check the above code and let me know if it works for you.

| improve this answer | |
  • 1
    This shortcode is echoing its output, meaning it won't appear in the correct place. Shortcodes need to use return (but not inside a loop). – Jacob Peattie Nov 20 '17 at 10:17
  • @JacobPeattie then where should I put return? The Code given by Pratik is working well for my output – Dharit Soni Nov 20 '17 at 10:18
  • You need to either build up a variable by concatenating it during the loop, or output buffering. See my answer. – Jacob Peattie Nov 20 '17 at 10:21
  • 1
    @jacob it is not necessary to have a return in your function. In this case it was not required so he have not used it. – user111017 Nov 20 '17 at 10:21
  • @Jumper Yes, return is required. See documentation for the shortcode API. echo will work in some contexts, but it's still incorrect. – Jacob Peattie Nov 20 '17 at 10:22
2

Shortcodes need to return their output, otherwise the output will appear whenever the content that contains it is processed, rather than where inside the content the shortcode actually appears.

The simplest way to achieve this is to use output buffering to capture the output with ob_start() and then return what it captured with ob_get_clean():

function blog_post_home_shortcode() {
    ob_start();

    $query = new WP_Query( array(
        'post_type'      => 'post',
        'order'          => 'DESC',
        'post_status'    => ' publish',
        'posts_per_page' => 3
    ) );

    while ($query->have_posts()): $query->the_post();
        the_title();
    endwhile;

    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode( 'blogs_home', 'blog_post_home_shortcode' );

See the documentation for more (emphasis mine):

The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.