treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Wordpress - query_posts issue

  • I'm trying to display a list of recent posts from a specific category. I've been scouring all over the interweb looking for solutions, and everything I try only works half way.

    No matter what I do, the resulting list displays 6 posts, regardless of what I set it to. Both of the following seem to work, yet they both return 6 posts. At first I thought I must be missing something, but after trying countless different options, and noticing that it's always 6 posts, I'm thinking there's something else at work. Any ideas?

    <ul>
    <?php query_posts('category_name=portfolio &posts_per_page=4'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; endif; ?>
    </ul>

    <?php
    $recentposts = get_posts('numberposts=3&category_name=portfolio');
    foreach ($recentposts as $post) :
    setup_postdata($post); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
  • In the first query_posts there should be no spaces after 'portfolio'.

    Also, you should be clearing your query_posts with:
    <?php wp_reset_query(); ?>
  • I noticed the blank space as well but figured that wasn't the problem (at this point my priority is to just get it working).

    I'm also very rusty right now, but I don't remember having such issues with simple things like this.

    Is this more proper?
    <ul>
    <?php query_posts('category_name=news&posts_per_page=3'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; endif; ?><?php wp_reset_query(); ?>
    </ul>


    Still doesn't work. There's 4 posts in that category and all 4 are listed rather than 3
  • Try showposts=3 rather than posts_per_page=3
  • PERFECT! That seems to do the trick.

    Thanks for the help to both of you!