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
add comment

1 Answer

up vote 1 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
add comment

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.