0

In my theme's file single.php, there is this part of code to get Related Videos:

      <?php
      if ( $wp_query ->have_posts() ) {

        while (have_posts()) : the_post();
          get_template_part( 'preview', get_post_format() );
        endwhile;

      }

      wp_reset_postdata(); 
    ?>

But this get the entire loop of all posts on the website. I'm wondering, is there a way to limit this and display for example 10 posts? Thanks a lot.

3

You need the argument post_per_page to limit the posts.

Before your loop it should be something like this.

$args = array (
    'post_type'              => 'custom_post',
    'posts_per_page'         => '10',
);

$wp_query = new WP_Query( $args ); 


// your loop 
if ( $wp_query ->have_posts() ) {

    while (have_posts()) : the_post();
      get_template_part( 'preview', get_post_format() );
    endwhile;

}

wp_reset_postdata(); 

If it is the custom post, make sure to replace custom_post

  • I should only replace custom_post to post, and now its working like a charm! Thanks a lot!!! – Devilix Apr 9 '18 at 12:40
  • I'm glad I was able to help. Here, If it's a default wordpress post, it's not necessary to add 'post_type' => 'post'. – Ronald Apr 10 '18 at 5:41

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.