Treehouse: Grow your CSS skills. Land your dream job.

One loop vs multiple loops on static homepage

  • # February 9, 2011 at 5:20 pm


    I have a static homepage using multiple loops to display each category with 3 random post entries. I’m using something like the following:

    But because I have 12 categories, I’m using 12 query post loops to generate the content. I was wondering if there was a more efficient way of doing it? Thank you!

    # February 10, 2011 at 3:58 am

    The second line in your code, this one:

    < ?php query_posts('cat=6&orderby=rand&showposts=3'); ?>

    has a deprecated parameter (showposts) in it, according to the query_posts function reference. Try using posts_per_page=3 instead of showposts=3.
    That should display 3 posts per page, but I think those will be just the last 3 ones, instead of 3 random ones like you want. Worth to try though.

    # February 10, 2011 at 9:58 am

    I don’t think that’s what he’s asking, what he has should give 3 random posts from a category but he had to use that query post function with each loop 12 times, once for each category. I don’t know whether there’s a way to achieve this with just 1 loop/query post – I haven’t seen anything while browsing the codex, but if someone else has a solution then I’d be very interested to hear it too!

    # February 10, 2011 at 10:06 am

    @Bob I’m not having a problem with it displaying 3 random posts using showposts. But I probably should switch to using the non deprecated call. Thanks!

    @Johnnyb Yes I’m wondering if there’s a way to use one loop to do it all, instead of 12 separate loops. The homepage does 143 queries in just under 2 seconds. At least that’s what I’m getting for the output using:

    < ?php echo get_num_queries(); ?> queries. < ?php timer_stop(1); ?> seconds.

    But if one loop could be achieved would it make it quicker?

    # February 13, 2011 at 6:41 pm

    How about a different question?! When using multiple query_posts loops, should you use the wp_reset_query after each one? I haven’t used it and see no issues. What benefit does it have?

    # February 13, 2011 at 6:48 pm

    I don’t know if I’m talking out of my bum on this, but im n00bish to PHP, are while statments safe from web damages? Like I mean is their anyway your having some type of try-catch for queried calls?

    Logically I would do a function to grab the row count for each query then random count something and print out the random numbers…

    I’m sure it’s not helpful but just a thought!

    # February 24, 2011 at 4:03 pm

    I wanted to come back to this topic and get some feedback. I was reading on WordPress that if you’re using multiple loops you should use get_posts instead of query_posts.

    What does everyone think? Is one better than the other?

    # February 24, 2011 at 8:30 pm

    I can’t answer that, but I would like to know the answer. I myself have a question on looping and was wandering if any of you could take a look at it & give me a response. I was thinking I might find the answer her…

    My Post

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.