Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:46:02+00:00 2026-06-03T18:46:02+00:00

My goal here is to use multiple wordpress loops to style each post in

  • 0

My goal here is to use multiple wordpress loops to style each post in a given category separately. I do believe I have it mostly figured out except for the actual query..

I need to be able to query the {MOST RECENT} post in a category in the first loop, then on the second loop query the 2nd most recent post in a category, then the 3rd most recent post in the next loop, enabling me to have separate classes & styles for each.

Any help would be amazing +++!!

<?php if (have_posts()) : ?>
           <?php query_posts('category_name=Main&posts_per_page=1&={MOST RECENT}'); ?>
               <?php while (have_posts()) : the_post(); ?>    

                            <div class="row1">
                                <div class="one">
                                    <div class="post_data">
                                        <div class="icons_right"><img src="pop_out_icon.png" alt="pop out icon" /></div>
                                        <h1 class="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                                        <h2 class="post_snippet">Lorem Ipsum Dolar Sit Amet.</h2>
                                        <p class="post_date"><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></p>
                                    </div> <!-- post_data //-->
                                <?php the_content(); ?>
                            </div>  <!-- 1 //-->
               <?php endwhile; ?>

                 <?php query_posts('category_name=Main&posts_per_page=1&={SECOND MOST RECENT}'); ?>
               <?php while (have_posts()) : the_post(); ?>    

                            <div class="row2">
                                <div class="two">
                                    <div class="post_data">
                                        <div class="icons_right"><img src="pop_out_icon.png" alt="pop out icon" /></div>
                                        <h1 class="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                                        <h2 class="post_snippet">Lorem Ipsum Dolar Sit Amet.</h2>
                                        <p class="post_date"><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></p>
                                    </div> <!-- post_data //-->
                                <?php the_content(); ?>
                            </div>  <!-- 2 //-->
               <?php endwhile; ?>

     <?php endif; ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:46:04+00:00Added an answer on June 3, 2026 at 6:46 pm

    I notice that although you want to use different loops to define unique classes, your loop blocks are largely the same. If all you want to do is change the classes of your elements, there’s no need to use three separate loops, as that will clutter up your template and end up being much slower than using a single loop.

    You should also avoid using query_posts, as it overrides the default WordPress Loop, and may have unintended consequences, especially if you forget to reset the query.

    The order of the posts in the loop defaults to the most recent posts, so you don’t need to worry about setting the ordering parameters.

    Using your example, I’ve reworked everything to apply dynamic classes to your wrappers depending on how many iterations the loop has gone through. Keep in mind, you can use attributes of the post itself to define your classes to make them unique (in this case, the post ID is used):

    <?php
    $count = 0;
    $postsPerRow = 4; //<-- This will help set your top wrapper
    $query = new WP_Query('category_name=Main&posts_per_page=3');
    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
    if($count<(floor($query->found_posts/$postsPerRow)*$postsPerRow)){
        $open = !($count%$postsPerRow) ? '<div class="row row-'.(floor($count/$postsPerRow)+1).'">' : '';
        $close = !($count%$postsPerRow) && $count ? '</div>' : '';
        echo $close.$open;
    ?>    
        <div class="<?php echo "loop-$count post-".get_the_ID(); ?>">
            <div class="post_data">
                <div class="icons_right">
                    <img src="pop_out_icon.png" alt="pop out icon" />
                </div>
                <h1 class="post_title">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </h1>
                <h2 class="post_snippet">Lorem Ipsum Dolar Sit Amet.</h2>
                <p class="post_date"><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></p>
            </div> <!-- post_data //-->
            <?php the_content(); ?>
        </div>  <!-- 1 //-->
    <?php
    $count++;
    }
    endwhile;endif;
    echo $count ? '</div>' : ''; //<-- Close row wrapper
    ?>
    

    UPDATE: Now your top wrapper will store 4 posts per row. This can be adjusted however you need through the $postsPerRow variable, and you can always increase the Posts_per_page parameter as needed.

    EDIT 2: Using WP Query has the added benefit of separating out values you might need. Review the code for an update to your latest question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project here the goal is to merge multiple Access DB's into
Here's my ultimate goal in all of this. I have a viewcontroller with a
For example, my goal is to test the code given here: PHP script that
I have a QWidget that contains multiple children. The ultimate goal is to be
I have an application that has multiple states, with each state responding to input
I have a jsFiddle here: http://jsfiddle.net/dztGA/22/ The goal: Essentially, I'm trying to have 2
My goal here is to override a method if it isn't found, otherwise use
My goal here is to make a button. I want the text to sit
My goal here is to open temp.php, explode by ### (line terminator), then by
The goal here is to take some list of coordinates, like [[1,2],[3,4],[7,1]] , then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.