Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
    
i am new in this filed i want to use this code in my website,but i don't know where i use this code in my website. Please help me. Thanks –  sammy Jan 12 at 8:39
    
To post questions please don't use the answers. –  cybmeta Jan 12 at 8:58
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  birgire Jan 12 at 9:23
    
For context the Answer by @sammy was converted to a comment, I've preserved the responses for sammys benefit, ps: welcome to the community :) –  Tom J Nowell Jan 12 at 9:39

2 Answers 2

up vote 2 down vote accepted

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;
share|improve this answer
    
That's what happens when you cut and paste from the web. Thanks for the update! It works!! –  TruMan1 Feb 10 '13 at 5:17

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;
share|improve this answer
1  
showposts was dropped in favor of posts_per_page. Please read the accepted answer –  Pieter Goosen Jan 23 at 10:57
    
okay ..but showposts also worked in my case. –  Mayank Kushwaha Apr 1 at 10:04
    
Yes, it does, but it is depreciated it might get removed in future which will break your site –  Pieter Goosen Apr 1 at 10:06
    
ok.. Thanks buddy :) –  Mayank Kushwaha Apr 1 at 10:08
    
Please reread my comment, do not use showposts, it is depreciated –  Pieter Goosen Apr 1 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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