Why WP_Query(‘showposts=5’) shows only 1 post?

I am trying to do a simple query to get the latest 5 posts into an unordered list, but this is only showing 1 result even though I have several posts. I even did an offset, but it shows the next post yet still 1 result. What am I doing wrong?

<ul>
    <?php $the_query = new WP_Query('showposts=5'); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
        <li>
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
            <p><?php the_content_limit(250); ?></p>
        </li>
    <?php endwhile;?>
</ul>

Related posts

Leave a Reply

2 comments

  1. the_content_limit does not exist in WordPress. You probably want something like the_excerpt.

    What’s likely happening is your loop is working fine, but the call to an undefined function causes the program to error out, making it appear the the loop is not working. Look at the rendered HTML: you’ll probably see a single, opening <li> tag, the link and an opening paragraph tag.

    showposts is also deprecated. Take a look in the codex: dropped in 2.1

    Try this:

    <?php
    $query = new WP_Query(array(
        'posts_per_page'   => 5,
    ));
    
    while ($query->have_posts()): $query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <p><?php the_excerpt(); ?></p>
        </li>
    <?php endwhile;
    
  2. The default syntax for post_per_page is-

    <?php
          $query = new WP_Query
          (array(
                     'posts_per_page'   => 5,
                 )
           );
    
           while ($query->have_posts()): $query->the_post(); ?>
          <li>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <p><?php the_excerpt(); ?></p>
         </li>
    <?php endwhile;