I've created a WordPress loop in PHP for a custom post type with 3 columns in the 1st row and 4 columns in the rest of the rows. However, with my current code, the 3rd item from the 1st row repeats itself in the 2nd row which is not ideal. How can I ensure that my 4-column loop starts at the 4th item. Right now it seems to be starting at the 3rd even though I've specified greater than or equal to 4 in my code ($i >= 4). I can't figure out what I'm doing wrong in the code. Any help would be strongly appreciated!
Here is a screenshot of what I mean when I say 3rd item repeats: https://i.stack.imgur.com/eA0J6.png
Also, here is my current code:
<?php $i = 1 ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($i <= 3 ): $i++;?>
<div class="col-md-4">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php endif; ?>
<?php if($i >= 4): $i++; ?>
<div class="col-md-3">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php endif; ?>
<?php endwhile; endif; ?>
When$i
is exactly equal to 3, it will be incremented to 4 in the firstif
block, then the secondif
will also be true. Try this:
<?php if($i <= 3 ): ?>
<div class="col-md-4">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php else: ?>
<div class="col-md-3">
<?php get_template_part( 'loop-templates/content', 'activity' ); ?>
</div>
<?php
endif;
++$i;
?>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.