Support » Fixing WordPress » using get_posts before loop

  • I’m working on a theme from a design at, and the design has a list of recent posts above the header. I’m able to get two columns of 3 posts titles with links no problem using the get_post template tag:
    $posts = get_posts('numberposts=3');
    foreach($posts as $post) :
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    and adding an offset= in the second column.
    However, no matter what I do, I’m getting a mysql database error when the page lists the posts in the content. Remove the get_posts queries, and the error goes away.
    Example here
    What am I missing. This code works fine in the sidebar, can it simply not be used before the loop?
    EDIT I am using 2.0 on this testbed site, but the error occured on a 1.5.2 site as well.

Viewing 6 replies - 1 through 6 (of 6 total)
  • To update, with a lot of help of alphoide via IRC, we got to the point using a query_posts before the full “loop” in my index.php file made all the DB errors go away, however, when clicking on the first post to get to single post view, it takes you to the post from the offset. So basically, I need to figure out how to “un-offset” the posts afterward. any suggestions. Otherwise, I’m gonna scrap the second loop and put something else in the element.

    I may be misunderstanding the problem but it seems to me either a multiple loop question or more like a simple multiple sidebar question.

    I would just introduce a new instance of sidebar and position it according to the design.
    It is really an unusual positioning of a sidebar but seems like the most natural approach.

    Actually kassad, it’s a multiple loop question. I’ve changed the elements around, so actually the example is longer valid. The design is the same, but i’ve moved the tag cloud to the area which I was trying to run the two get_posts loops. Ultimately what was happening, was everything was good, but the second get_post loop was using an offset. Then when I ran my “normal” loop in my index.php, and you clicked on the first post for single post view, the permalink would be right, but it was taking you to the offset post, that is, if my offset was 3 in my header, second get_post loop, then when you click on the first post of the index section, it was taking you to the 4th post. It’s very confusing to explain, and more so for my skill level, thus a reuse of the space.

    Ah, now that I re-read what you are saying, rather than code the space in the header, which comes before the loop in the index.php, treat it as sidebar and position it above the content via CSS. Possible, but I’m liking the idea of the tag cloud up there anyway. But thanks for offering the suggestion. Still doesn’t fix the multiple loops dilemma.

    I have not yet used this method so I have not much to offer.
    Though there are twe threads discussing similar problem:

    This, at the end, refers to:

    yeah, I’m guessing the best option would have been to scrap the get posts, and use query_posts and the multiple loops models. I simply had never tried to run another loop after the get_posts, so I didn’t realize its “power” 😉

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘using get_posts before loop’ is closed to new replies.