I am trying to detect each 4th post to insert extra code in my layout in wordpress using modulus method but I just cant get it.
Here is a short example of mine:
<?php if (have_posts()) : ?>
<?php $count=0;?>
<?php while (have_posts()) : the_post(); ?>
<div class="column">
<!--content-->
</div>
<?php
if ($count % 4 == 0){
echo '<div class="clear"></div>';
}
$count++;
?>
<?php endwhile; ?>
<?php endif; ?>
all that is inside the while loop. What am I doing wrong? Thank you.
You need to start your counter at 1, as you are increasing it at the end of the loop:
<?php $count=1;?>
Either that, or you increase it at the start of the loop / before the check:
<?php
$count++;
if ($count % 4 == 0){
echo '<div class="clear"></div>';
}
?>
When it comes to things like this, I always increment one on the if statement before calling modulo like so:
if(($count+1)%4 == 0)
This way it's easy for me to make a mental note that the statement naturally reads "if the current count is the 4th one then do:"
Related
Currently I am working on a Wordpress website with the posts showing in two columns running with a PHP code. The problem I am facing is that the posts showing on the website are limited to 10. Does anyone know where in the code I can make a change to show unlimited posts? Or do I need a whole new PHP code for this?
Would like to hear from you!
This is the code I am using
<?php $i = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
if($i == 0) {echo '<div class="ng-row">';}?>
<div class="half">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php $i++; if($i == 2) {$i = 0;echo '</div>';}?>
<?php endwhile; else: ?>
<?php if($i > 0) {echo '</div>';}?>
<?php endif; ?>
I've tried to change the numbers but it didn't work out well..
For an unlimited return set the posts_per_page to -1
add_action( 'pre_get_posts', 'set_posts_per_page' );
function set_posts_per_page( $query ) {
// This will set the query to return all results
$query->set( 'posts_per_page', -1 );
return $query;
}
Although be aware that this can be very bad for performance depending on the amount of posts. It should really only be done if you have a limited result set and know what that is.
My count loop currently works but it is not working correctly. I need every second item to go into the right column and the rest stay in the left. So 1, 3, 5 etc in the column called split-left and 2, 4, 6 to go into the column called split-right
<!-- SPLIT EFFECT PAGE BUILDER -->
<div class="page-builder">
<?php if( have_rows('split_effect_page_builder') ): ?>
<div class="split-left">
<?php $i = 1; ?>
<?php while ( have_rows('split_effect_page_builder') ) : the_row(); ?>
<?php get_template_part('template-parts/page', 'builder'); ?>
<?php
if($i % 2 == 0){
echo '</div><div class="split-right">';
$i = 0;
}
$i++;
?>
<?php endwhile; ?>
</div>
<?php else : ?>
<?php // no layouts found ?>
<?php endif; ?>
</div>
<!-- END SPLIT EFFECT PAGE BUILDER -->
Seems you just need to reset your counter whenever it reaches 2..
<?php
if($i == 2){
echo '</div><div class="split-right">';
$i =0; // set back to zero...
}
$i++; // and now it's 1 again so next iteration would be left aligned
?>
PS.. not sure why you close a PHP tag only to immediately re-open it.. That won't break anything, but probably a good habit to get rid of.
statmentI am currently working on a project. I am having issues with making a variable ++ in the loop while nested in if statements. I really do not understand why it's doing what it's doing. Here is my code.
<?php $i=0;
while ($loop->have_posts() ) : $loop->the_post(); ?>
<?php if(get_field('featured_game') && valid_release(get_field('release_date'))):?>
<div class="item <?php if($i == 0){echo 'active';}?> row carousel-<?=$i;?>">
// do generated html
<style>
.carousel-<?=$i;?>{
// do generated css
}
</style>
</div>
<?php $i++;?>
<?php endif?>
<?php endwhile;?>
</div>
My results have
carousel-
these spots all having returned 0 despite the loop running. meaning that incrment is not happening.
if I remove the if statement the loop works, as expected. The if statement are just to verify criteria to be true. I do can place the ++ outside of the if statement I get the same results.
Could some please explain what I am missing here? This seems like such a basic problem. I really do not understand why $i will not increment when does meet the criteria and posts the correct info. Thanks in advance to anyone who takes the effort.
I ran this code:
<?php $i = 0;
while ($i < 10) : ?>
<?php if (true): ?>
<?php if ($i == 0) {
echo 'active';
} ?>
<?= $i; ?>
<?php $i++; ?>
<?php endif ?>
<?php endwhile; ?>
and the result was:
active 0 1 2 3 4 5 6 7 8 9
so either
get_field('featured_game')
or
valid_release(get_field('release_date'))
must be false.
Try
var_dump(get_field('featured_game'));
var_dump(valid_release(get_field('release_date')));
inside the loop.
Turns out, it was increment properly all along.
My issue turns out to be that bootstraps carousel cycles the the content within the the parent div not the actual div it seems. If I disable it, or look directly at the source, everything is what it is supposed to be.
My loop displays posts in two columns using this:
<?php
if (have_posts()): while (have_posts()) : the_post();
$count++;
?>
<?php if ($count == 1) : ?>
<div class="home-ci-row">
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div>
<div class="home-ci-gap"></div><!-- /* the gap */ -->
<?php elseif ($count == 2) : ?>
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div> <!-- main-column-item-wrap -->
</div><!-- /* home-ci-row*/ -->
<?php $count = 0; ?>
<?php else : ?>
// No posts
<?php endif; endwhile; endif; ?>
You can see that the <div class="home-ci-row"> opens in the first count & closes in the second one </div>
so when my loop has an even number of posts works great, but with odd number it doesn't close the div
so My idea is this: If loop has even number
If loop has odd number of posts
By the way, you can do something like:
<?php
$count=0;
while(have_posts()){
if($count%2==0){
echo '<div class="home-ci-row">';
//draw your left div here
}else if($count%2==1){
//draw your gap here
//draw your right div here
echo '</div>';
}
$count++;
}
//close div if count is an odd number
if($count%2==1) echo '</div>';
?>
Is it possible to swap to a for loop? Is this what you need?
for ($i = 0; $i < $numberOfElements; $i++)
{
//if (odd number) && (this is the last element)
if (($i % 0 == 1) && ($i == $numberOfElements - 1))
{
//Special Case
}
else
{
//Normal Case
}
}
Caveat: Watch out for errors, PHP isn't my strongest language
This question was answered on WP Development StackExchange by fischi
Quoting from his answer:
You could do this much easier. As you are making a layout that can be achieved by floats, there is no need to declare a Row every second time.
In my Code example I just youse the $count to determine the Class of the HTML-Element. In combination with the total Number of Posts displayed.
If the total number of posts $wp_query->post_count is reached by the $count AND the total number is odd, I give the Element the Class fullwidth. In the same way, I determine if it is the first or the second (see the IF-Statement).
All I need to do afterwards is output the corresponding Class for each HTML-Element in the Loop. Besides the Class, no Element is diffferent from each other.
I use the Modulo Operator in PHP ( % ) to determine odd/even. It delivers 1 if I use $count % 2 and $count is odd. If you are not sure about this operator, read about it here.
So your code could look like this:
<?php
$count = 0;
if (have_posts()): while (have_posts()) : the_post();
if ( ++$count == $wp_query->post_count && ( $wp_query->post_count % 2 ) == 1 ) {
// if final count is reached AND final count is odd
// full width item
$postclass = "fullwidth";
$opentag = '';
$closingtag = '</div>';
} else if ( ( $count % 2 ) == 1 ) {
// if $count is odd it is the first item in a 'row'
$postclass = "halfwidth first";
$opentag = '<div class="home-ci-row">';
$closingtag = '';
} else {
// second item in a row
$postclass = "halfwidth second";
$opentag = '';
$closingtag = '</div>';
}
?>
<?php echo $opentag; ?>
<div class="main-column-item-wrap <?php echo $postclass; ?>">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div><!-- main-column-item-wrap -->
<?php echo $closingtag; ?>
<?php endwhile; endif; ?>
I am trying to do an if/else-statement on my WP-template. But i can't seem to pick the right element.
I wan't it to look for the latest post on the blog. I already added a script that add a class of .first to the latest post.
What im trying to do in dummy-php-language:
<?php
if ( class is .first) { do something to .first}
else () { do something to everything else }
?>
i've looked in the wp-documentation. But i can't seem to find anything that will select "latest post"...
Thanks
Jonas
A very simple solution would be to keep a count of the post you're at, and do something if it's the first. In the index.php, in the have_posts() loop:
<?php if (have_posts()) : $count = 0; ?>
<?php while (have_posts()) : the_post(); $count++; ?>
<?php if($count ==1) ?>
// do something to first
<?php else ?>
// do something to everything else
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>