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 8153111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:54:40+00:00 2026-06-06T15:54:40+00:00

Problem. What I am trying to do is to call a category specific loop,

  • 0

Problem.
What I am trying to do is to call a category specific loop, however, I want whats returned to display from most recent first, to be numerically numbered so that for every 2 that is displayed, will echo a css class ascertained to them and the 3rd result to display a completely different class as this is how I have written my html. Here is what I am trying to get the HTML to display:

<div id="content">
    <div class="block1"></div>
    <div class="block1"></div>
    <div class="block2"></div>
    <div class="block1"></div>
    <div class="block1"></div>
    <div class="block2"></div>
</div>

If there are more results then the first two will be named in the first div and the 3rd of all the results will have that class name assigned to it.
Help would be more than greatly appreciated.

Remarked:

<?php query_posts( 'cat=featured&showposts=4' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php foreach($recent as $index => $postObj) {
  $class = $index + 1 % 3 === 0 ? 'block2' : 'block1'; 
}
?>
<h1><?php the_title(); ?></h1>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

<?php get_footer(); ?>

however its returning the number of posts but under the posts its returning
Warning: Invalid argument supplied for foreach()
Have tried trial and error however, I think that my grammar is atrocious.

  • 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-06T15:54:42+00:00Added an answer on June 6, 2026 at 3:54 pm

    What you’re looking for is the modulo operator. What modulo does is find the remainder of a division operation. In effect the result is in a range of 0..N-1, where N % N = 0.

    foreach($posts as $index => $postObj) {
      $class = $index + 1 % 3 === 0 ? 'block2' : 'block1';
    

    This accomplishes what you want because the loop logic looks like:

    1 % 3 = 1 -> block1
    2 % 3 = 2 -> block1
    3 % 3 = 0 -> block2
    

    Your code needs to be:

    <?php
      query_posts( 'cat=featured&showposts=4' );
      $index = 1;
    
      if ( have_posts() ) :
        while ( have_posts() ) : the_post();
    
        $class = $index++ % 3 === 0 ? 'block2' : 'block1'; 
    ?>
    <div class="<?php echo $class ?>">
      <h1><?php the_title(); ?></h1>
    </div>
    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    
    <?php get_footer(); ?>
    

    The $index++ operator means, "increment $index after this use of it." So, notice how the loop is set up. Before the loop we set $index to 1. Inside the loop we set $class using our modulo technique, then increment $index. Then we have to create a container DIV, like you mentioned, and echo the class there.

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

Sidebar

Related Questions

I'm running into this problem when trying to call a SOAP Web Service from
I'm having a never-ending problem with trying to call a stored procedure from a
I'm having a problem while trying to call a custom Model method from my
I am trying to call my BHO method from the javascript. The problem is
I am trying to call $this->getIsAnchor() from a category template file to show certain
I'm trying to call an ImageMagick command from Python 2.7 using subprocess.call. My problem
I'm facing a problem trying to call Compile() on the LambdaExpression of type Expression<Func<MyType,
I am trying to call the stored procedure using C#. I am facing problem
I'm having a problem trying to getline lines from a .txt file. I used
I'm trying to make category changes with Jquery & Php. I have no problem

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.