Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End One loop vs multiple loops on static homepage

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #31566
    jacorre
    Participant

    Hello,

    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!

    #61433
    Bob
    Member

    The second line in your code, this one:

    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.

    #61366
    Johnnyb
    Member

    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!

    #61368
    jacorre
    Participant

    @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:

     queries.  seconds.

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

    #60671
    jacorre
    Participant

    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?

    #60672
    Vinnix
    Member

    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!

    #58493
    jacorre
    Participant

    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?

    #58407

    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)
  • The forum ‘Back End’ is closed to new topics and replies.