Wordpress Loop For Even/Odd Posts - php

I have two columns and I want one post type to get distributed to each column evenly. So it’s two side-by-side divs and I want:
Div1 = Post1, Post3, Post5
Div2 = Post2, Post4, Post6
So basically get odd/even posts. Not exactly sure how to do it.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="column1">
<?php
//Get Odd Posts
?>
</div>
<div class="column2">
<?php
//Get Even Posts
?>
</div>
<?php endwhile; ?>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>

You want to use the modulus operator something like this:
<?php
$i = 0;
for ($i = 0; $i <20; $i++){
$class = $i % 2 == 0 ? "even" : "odd";
echo "<div class='" . $class . "'>";
echo "</div>";
}
?>

To do what you want, first you have to store the results somewhere (as even/odd), then display them.
Though you should really target these posts with CSS, not PHP, as it's hackish at best.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php
$i = 0;
while (have_posts())
{
$key = $i & 1 ? 'odd' : 'even';
$post[$key] = array(get_the_title() => get_the_content());
$i++;
}
?>
<div class="column1">
<?php foreach ($post['even'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<div class="column2">
<?php foreach ($post['odd'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>

Related

Use $I++ more than once in a loop

I need to use the value for $i++ in numerous places but if I do that then some fields end up skipping values and instead of being 1, 2, 3 etc. they are 1, 3, 5 and the other field has values of 2, 4, 6 etc.
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i++; ?></div>
<div>Should be the same as this one: <?php echo $i++; ?></div>
<?php end while; ?>
It works if I create another variable but this feels like a hack.
<?php $i = 1;
$x = 1;
?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i++; ?></div>
<div>Should be the same as this one: <?php echo $x++; ?></div>
<?php end while; ?>
Is there a better way?
You can do the increment as a discrete step at the end of the loop, this means you can use it in various places without wondering when you changed the value...
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ):
the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i; ?></div>
<?php $i++; ?>
<?php end while; ?>
The solution is simple - increase $i only once:
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i++; ?></div>
<?php end while; ?>
A clean approach would be to echo the value of $i in your loop, and only increase it at the end, as a step alone. This is a good approach to have not only in PHP but in programming in general.
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i; ?></div>
<?php
$i++;
end while; ?>

Adding div class based on PHP variable from ACF repeater field?

I am looping through a repeater field in Advanced Custom Fields and displaying divs for each item in the repeater. If the index is equal to 0, I want to add a special class to just that div. Is this possible? Here's what I've tried:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div class="product <?php if ($i == 0) { echo 'active-class'; } ?>"></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>
Unfortunately this is not working.
Instead of doing the conditional within the class, I just did it on the outside and defined two different class tags based on the condition:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div <?php if ($i == 0) { ?>class="product active"<?php } else { ?>class="product"<?php } ?> ></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>

PHP loop interject

Consider the following loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php endif; ?>
<?php endforeach; ?>
I wanted to wrap the first 12 items in a div and then the last 2 items in a div. The problem is there are not always 12 items exactly in the first div. There can be between 2 AND 12 items.
How would I manipulate this loop to achieve such? Many thanks
Just use a counter inside the loop to see how many times you've been through it.
<?php $count = 1; ?>
<?php $break= count($this->item->extra_fields) - 2; ?>
<?php echo "<div>"; ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php $count++; ?>
<?php endif; ?>
<?php if ($count == $break) : ?>
<?php echo "</div><div>"; $count ==0; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

How can I show four images per row and rows can be of any no with php

Dear Friends I have a Div of Images.
<div class="img_team_container">
<div class="img_team_subcontain">
<div class="img_team"></div>
</div>
</div>
My question is that How can I show four images per row and rows can be of any no with php.
Assuming you have a array $images of images:
<?php $i = 0; foreach($images as $image): ?>
<?php if($i === 0): ?>
<div class="row">
<?php endif; ?>
<?php echo sprintf('<img src="%s" />', $image['src']); ?>
<?php if($i === 4): $i = 0; ?>
</div>
<?php else: $i++; endif; ?>
<?php endforeach; ?>
well i dont see an image tag in your code but use modulo arithmetics.
<?
$perRow=4;
for($i=0;$i < count($myimages); $i++) {
echo '<img src="'.$myimages[$i].'"/>';
if(($i+1)%$perRow === 0) {
// we reched the end of the row, lets break
echo '<br/>';
}
}
?>

Categories