Wrap outputs of a Wordpress query every second item - php

I think I am pretty close to the solution but I cannot figure out the logic for where I should close the last div.
I have a wordpress query which requests four posts.
I want to wrap every two items in <section class="sponsor-row">
So I have implemented a counter in my query that counts each post, and if it finds two posts have been output it closes the <section> div off.
But I cannot work out how I should work out if the row should be closed again.
Can anyone figure this out? How can I make it wrap every two outputs in <section class="sponsor-row"> ?
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$counter = 0;
echo '<section class="sponsor-row">'; // Opening the section here
if ( has_post_thumbnail() ) {
$counter++;
echo '<div class="grid half">';
echo '<a class="sponsor" href="'.get_the_permalink().'" target="_blank">';
the_post_thumbnail( 'full' );
echo '</a>';
echo '</div>';
if ($counter <= 2){
echo '</section>'; // closing the section if two posts
$counter = 0;
}
}
}
}
Full query here: http://pastebin.com/e6RzZLR5

if you do if ($counter <= 2){ then it will close it each time it is less or equal 2, which means it will close it twice for each item. You should use if ($counter == 2){ and $counter = 0; should be at the top before the query, otherwise you will set it to 0 each loop.
See comparison in php

Related

PHP while loop paging

I have the following PHP loop:
if( have_rows('home_projecten') ) {
$i = 0;
while( have_rows('home_projecten') ) {
$i++;
the_row();
if($i == 1) {
echo '<div class="slide">';
}
echo '<div class="project"></div>';
if($i == 3) {
$i = 0;
echo '</div>';
}
}
}
I am trying to split my 'project items' in slides, with 3 items per slide. Currently, this is working fine for every 'full' slide, meaning a slide with 3 projects. When $i becomes larger than 3, a new slide is added.
However, when I have for example 4 projects, the second slide div is not closed anymore, as I never reach $i == 3 again.. I know I could fix this with counting the array and doing something with that, but I have no idea how..
Help would be greatly appreciated!

Get on every 4th post in WordPress loop

I am currently trying to a create a feature on my category pages where on every 4th preview post, within the category an advert block is display.
essential it will work the following way:
post 1
post 2
post 3
post 4
ADVERT BLOCK
Post 5
Post 6
In my <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I have the following:
<?php $i = 0; ?>
<?php if (post_num ($i%4 == 0) < (5 - $featured_count)) : echo "this works"; endif ?>
can someone guide me in the right direction
Your more or less there, here's a quick example just showing some arbitrary text or the post title:
<?php
$counter = 0;
if (have_posts()) {
while (have_posts()) {
$counter++;
the_post();
if ($counter % 5 === 0) {
echo 'Advert Here!';
} else {
the_title();
}
}
}
?>
I haven't tested this, but just to give you an idea.
Edit: I should note that since you're looking to insert the advert after the fourth post, $counter % 4 won't do what you think, if you want to show four posts and then the advert, it would be placed in the 5th "position", hence $counter % 5.

Implementing a PHP counter inside of a while loop to wrap list items with ul elements

I am having difficulty implementing a PHP counter, to wrap my list elements in unordered list tags.
I want to wrap around every 3 list elements.
I have been trying to use these previous questions as a guide but to little avail.
easier way to get counter from while loop?
php loop counter bootstrap row
<?php
$counter = 0;
echo '<ul class="products latestCourses">';
while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php $counter = $counter++; ?>
<?php if ($counter%3 == 0) echo '</ul><ul class="products latestCourses">'; ?>
<?php endwhile; // end of the loop. ?>
This is what I have so far the page template simply contains a list element.
Currently this code is wrapping every list item in an unorder list.
<?php $counter = $counter++; ?>
this line is wrong, use either
<?php $counter++; ?>
or
<?php $counter = $counter +1; ?>
Use this for increment by 1
$counter = $counter + 1;
or
$counter = $counter + n;
where 'n' is the desired number by which you want to increment

Print new row after n divs

I am struggling to find a solution for this problem:
<?php
$counter = 1;
while (has_sub_field('company_members')) :
echo '<div class="row">';
while ($counter <= 3) :
get_template_part('team-member-box'); ++$counter;
endwhile;
echo '</div>';
$counter = 1;
endwhile;
?>
What I want the code to do is to print 3 "team-member-box" and then create a new row.
So far all the boxes have been collocated in one single row.
The code right now prints the same box for three times, instead I want it to take 3 elements in the repeater field "company_members" and print within a new row the next 3 elements.
Thank you in advance for your help.
Not sure if this is what you try to do but here is a general way to display list of data in 3 per row:
$counter = 0;
echo '<div class="row">';
while (has_sub_field('company_members')) {
echo ($counter++ % 3 == 0) ? '</div><div class="row">' : '';
get_template_part('team-member-box');
}
echo '</div>';
Explained:
has_sub_field it loops all company_members, I guess get_template_part renders some html using the current item. See the_sub_field('content') in the above link.
The $counter is not related to above behavior it is just used to separate the results in chunks of 3 and wrapping them in divs.
Thanks for this. And to fix the issue of creating an empty row at the beginning add this:
if($counter) :
echo ($counter++ % 2 == 0) ? '</div><div class="row">' : '';
else :
$counter++;
endif;

Conditional styling for post containers in The Loop

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; ?>

Categories