I want to break the loop into half of the total loop count. If the total loop count is odd, I would like to remove the last array item in order to make the total loop count even and add the deleted item to the second half.
Here is the code structure to demonstrate the loop-
<?php
$faqs = new WP_Query( array (
'post_type' => 'faq'
));
?>
<div class="row">
<div class="col-lg-6">
<!-- The first half of the tootal loop count. -->
<?php
while ($faqs->have_posts()) : $faqs->the_post();
the_content();
endwhile();
?>
</div>
<div class="col-lg-6">
<!-- The second half of the tootal loop count. -->
<?php
while ($faqs->have_posts()) : $faqs->the_post();
the_content();
endwhile();
?>
</div>
</div>
I don't know how to control the loop based on my mentioned conditions. That's why I couldn't able to try loop controlling.
In a single loop. Hopefully, it'll work. Couldn't test so let me if you face any situation.
<?php
$faqs = new WP_Query([
'post_type' => 'faq',
'post_status' => 'publish',
]);
$half = intval($faqs->post_count / 2);
$counter = 0;
?>
<div class="row">
<div class="col-lg-6">
<?php while ($faqs->have_posts()) : $faqs->the_post(); ?>
<?php if ( $counter === $half ) : ?>
</div><div class="col-lg-6">
<?php endif; ?>
<?php the_content(); ?>
<?php ++$counter; endwhile; ?>
</div>
</div>
try it like this
<?php
$faqs = new WP_Query( array (
'post_type' => 'faq'
));
?>
<div class="row">
<div class="col-lg-6">
<!-- The first half of the total loop count. -->
<?php
$i=0;
while ($faqs->have_posts() && $i < $faqs->post_count / 2) : $faqs->the_post(); $i++
the_content();
endwhile();
?>
</div>
<div class="col-lg-6">
<!-- The second half of the tootal loop count. -->
<?php
while ($faqs->have_posts()) : $faqs->the_post();
the_content();
endwhile();
?>
</div>
</div>
https://wordpress.stackexchange.com/questions/27116/counting-the-posts-of-a-custom-wordpress-loop-wp-query
Related
Sorry, it was hard to word this question. Below is the end result HTML I am trying to achieve with a WP Query. It's for a carousel that has 3 posts per slide.
<div id="carousel">
<div class="carousel-slide">
<div class="wp-post">Post 1</div>
<div class="wp-post">Post 2</div>
<div class="wp-post">Post 3</div>
</div>
<div class="carousel-slide">
<div class="wp-post">Post 4</div>
<div class="wp-post">Post 5</div>
<div class="wp-post">Post 6</div>
</div>
</div>
Basically, I need a way to have each individual post wrapped with a "wp-post" div, but every three posts wrapped in a "carousel-slide" div within a WP query of all posts. Any help is appreciated. Thank you!
<div id="carousel">
<?php
$args = array('post_type' => 'page',
'posts_per_page'=> -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
$i = 1;
while ( $the_query->have_posts() )
{
$the_query->the_post();
?>
<?php if($i == 1){ ?>
<div class="carousel-slide">
<?php } ?>
<div class="wp-post"><?php echo get_the_title(); ?></div>
<?php if($i == 3) { ?>
</div>
<?php $i = 1;}else{$i++;} ?>
<?php }
} ?>
I'm having an ACF repeater inside a custom post type loop with a counter that makes a row around every two col-md-6's. This works great when I have an even number of col's but not when it's uneven. When a post has an uneven number of col's the counter somehow remembers that for the next post and only displays one col in the first row.
Down here you'll find the current code and a little picture of what is happening.
Somehow I need to reset the counter after each post loop but can't figure it out. wp_reset_postdata doesn't seem to work.
<?php $args = array( 'post_type' => 'posttypename', 'posts_per_page' => '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Just a small change. You need to make sure that your counter is reset to 0 before the loop for the acf starts.
<?php $counter = 0; //Initialize your Counter here before the Loop Starts for each Post ?>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
I am trying to make Wordpress loop with php modulus. The scenario is wrapping every 3 posts in a div. My script below is working perfectly, except I found an unused empty div at the last (from inspect element).
$loop_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6
);
$loop = new WP_Query( $loop_args );
<?php if( $loop->have_posts() ): ?>
<div class="row">
<?php $count = 0; ?>
<div class="col-md-6">
<?php while( $loop->have_posts() ) : $loop->the_post(); $count++; ?>
<?php if( $count % 3 == 0 ): ?>
<article class="post-content"><?php the_title();?></article>
</div> <!-- .col md 6 -->
<div class="col-md-6">
<?php else: ?>
<article class="post-content"><?php the_title();?></article>
<?php endif; // end modulus ?>
<?php endwhile; // end while loop ?>
</div> <!-- .col md 6 -->
</div> <!-- .row -->
<?php endif; wp_reset_postdata(); ?>
Please take a look at the last highlighted div in my attachement:
screenshot preview
How can I remove that unused empty div?
Thanks in advance.
Try this:
<div class="row">
<?php $count = 0; ?>
<div class="col-md-6">
<?php while( $loop->have_posts() ) : $loop->the_post(); $count++; ?>
<?php if( $count % 3 == 1 && $count > 1 ): ?>
</div> <!-- .col md 6 -->
<div class="col-md-6">
<article class="post-content"><?php the_title();?></article>
<?php else: ?>
<article class="post-content"><?php the_title();?></article>
<?php endif; // end modulus ?>
<?php endwhile; // end while loop ?>
</div> <!-- .col md 6 -->
</div> <!-- .row -->
Here I don't close div on the third article but I open it if I need on the fourth.
The problem I see is that you are allways opeining the <div class="col-md-6"> when the modulus 3 is 0 even if there are no more articles after you open the tag. In such case you would end up with an empty div.
I was hoping one of you Wordpress gurus can help me out here.
I'm trying to create a loop within a loop that presents 12 posts, in 6 rows. In each row you have 2 divs. Each div is meant to display a post title. And the loop runs through all of the 12 posts and groups them correctly - 6 divs, with 2 posts. Each post has it's own unique title.
I've managed to get the loop to breakdown the 12 posts into 6 divs, each with two inner divs in there. But I can't get the inner divs to loop through all the posts. Instead they are just looping through the first two.
So what I end up with is 6 rows, each with two divs. But only the first two posts keep recurring throughout all the rows. What am I doing wrong here?
<!--TESTER -->
<!--TESTER -->
<!--TESTER -->
<div class="section section-testimonials">
<?php
$args=array(
'post_type' => 'testimonial'
);
$query = null;
$query = new WP_Query($args);
if( $query -> have_posts() ) {
echo '';
$i = 0;
while ($query -> have_posts()) : $query->the_post();
if($i % 2 == 0) { ?>
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'posts_per_page' => 2 ) ); ?>
<?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
<!-- Two posts appear here -->
<div class="col-md-6">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
</div>
<?php } ?>
<?php
$i++;
endwhile;
}
wp_reset_query();
?>
</div>
<!--TESTER -->
<!--TESTER -->
<!--TESTER -->
Any help would be incredibly appreciated!
Cheers,
Sanny
Here, I break the problem into pieces, so you can see the loops working on their own.
<?php
$slides = [0,1,2,3,4,5];
foreach($slides as $slide):
?>
<!-- I need to get 6 of these divs which contain 2 cards inside them -->
<div class="item active">
<p>Example Carousel Slide <?php echo $slide;?></p>
</div>
<?php endforeach?>
Then, instead of just echoing a constant <p> tag, you instead put a loop there.
<?php
$posts = ["post1", "post2"];
foreach($posts as $post):?>
<div class="col-md-6">
<?php echo $post;?>
</div>
<?php endforeach?>
I will jump ahead a few steps, but you end up with something like this.
<?php
$posts = [
0=>"post0",
1=>"post1",
2=>"post2",
3=>"post3",
4=>"post4",
5=>"post5",
6=>"post6",
7=>"post7",
8=>"post8",
9=>"post9",
10=>"post10",
11=>"post11"];
for($slideNumber=0; $slideNumber < 6; $slideNumber++):
?>
<!-- I need to get 6 of these divs which contain 2 cards inside them -->
<div class="item active">
<?php
echo("<p>This is slide number " . $slideNumber . "</p>");
for($i=0; $i<2; $i++):?>
<div class="col-md-6">
<?php
$actualPostNumber= ($slideNumber * 2) + $i ;
echo("<p>" . $posts[$actualPostNumber] . "</p>");
?>
</div>
<?php endfor; ?>
</div>
<?php endfor; ?>
Read that code, and try to come up with an expectation for what it will produce. In the end, you should end up with 6 slides, each containing two posts.
The code I posted is a skeleton relative to your eventual solution. However, hopefully this "counters" approach will assist you with assigning the correct number of posts to each carousel slide.
Thanks guys for all the help. I've managed to solve this!!! Here's the solution:
If anyone has any thoughts on making this a better solution lemme know! Cheers
<?php
$args=array(
'post_type' => 'testimonial',
);
$query = new WP_Query($args);
if( $query -> have_posts() ) {
echo '';
$i = 0;
$ids = array();
while ($query -> have_posts()) : $query->the_post();
if($i % 2 == 0) { ?>
<div class="item <?php if($i == 0) :?>active<?php endif; ?>">
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'posts_per_page' => 2, 'orderby' => 'rand', 'post__not_in' => $ids ) ); ?>
<?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
<!-- A single testimonial -->
<div class="col-md-6">
<div class="card card-plain">
<div class="content">
<h4 class="title"><?php the_content(); ?></h4>
<div class="footer">
<div class="author">
<span><?php the_title(); ?></span>
</div>
</div>
</div>
</div>
</div>
<?php $ids[] = get_the_ID(); ?>
<?php endwhile; ?>
</div>
</div>
<?php } ?>
<?php
$i++;
endwhile;
}
wp_reset_query();
?>
working on a site, and I have some difficulties. It's really hard or not done by php what I am trying to accomplish!
So, there is a section named 'references', and each item belongs to a row (out of 4 rows).
At my static page it looks like this:
<div id="references-tiles">
<div class="reference">
<div class="reference-figure malmberg"></div>
</div>
<div class="reference">
<div class="reference-figure malmberg"></div>
</div>
****<div class="references-row"></div>****
<div class="reference">
<div class="reference-figure port"></div>
</div>
<div class="reference">
<div class="reference-figure malmberg"></div>
</div>
****<div class="references-row"></div>****
<div class="reference">
<div class="reference-figure malmberg"></div>
</div>
</div>
The reference-row acts like a separator
Each reference has to be a post, unfortunately wordpress won't work this way
Loop through Post and display them one the same way (only the items)
<div id="references-tiles">
<?php
$query = new WP_Query( array( 'category_name' => 'References' ) );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID)); ?>
<div class="reference">
<div class="reference-figure" style="background-image: url('<?php echo $image[0]; ?>')"></div>
</div>
<?php endwhile; endif; ?>
</div>
I just can't figure out how to control in a way, that a reference item is added to a row.
I tried using some modulo statements like:
if ($referenceCount % 4 == 0)
elseif ($referenceCount % 3 == 0)
elseif ($referenceCount % 2 == 0)
else
But this wasn't working out because as long I am in the loop, each item can be devided but not added in an already existing row.
Is there a way? Maybe to loop through the loop, add each reference item in a array, and depending on how many items total, I can devid them. Or use the loop 4 times and depending on the modulo, add a reference-item to a row? But I guess this wil make the site a bit slower..
Sorry for bad English, I am not clear at al, I am happy to try en explain myself better!
Regards and thanks!
IT-student
You should add a counter to your loop.
<div id="references-tiles">
<?php
$query = new WP_Query( array( 'category_name' => 'References' ) );
// Add a counter vvvvv
if ($query->have_posts()) : $i = 1; while ($query->have_posts()) : $query->the_post();
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID)); ?>
<div class="reference">
<div class="reference-figure" style="background-image: url('<?php echo $image[0]; ?>')"></div>
</div>
<!-- If the counter is evenly divisible by 2, add a new row -->
<?php if ( $i % 2 === 0 ) : ?>
<div class="references-row"></div>
<?php endif; ?>
<!-- Increment the counter -->
<?php $i++; endwhile; endif; ?>
</div>
You can use array_chunk on the returned $query array
NOTE: The examples uses foundation's grid system to better display the outcome.
<?php
/**
* The second argument for array_chunk takes the size of the chunks
* ie: the number of columns you want to display per row
*/
$columnsPerRow = 4;
$query = new WP_Query( array( 'category_name' => 'References' ) );
if($query->have_posts()) : foreach(array_chunk($query->get_posts(), $columnsPerRow) as $posts) : ?>
<div class="row">
<?php foreach($posts as $post) : setup_postdata($post); ?>
<div class="columns small-6 medium-4">
<?php
/**
* Fetch the post thumbnail
* to be used as a background
*/
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
?>
<div class="panel" style='background:url("<?php echo $thumbnail[0]; ?>")'> </div>
</div>
<?php endforeach; ?>
</div> <!-- end row -->
<?php
endforeach;
endif;
wp_reset_postdata();
?>
You don't need to use a counter, wordpress has a counter as it is in their loop.
<?php if ($query->current_post%2) { ?>
<div class="references-row"></div>
<?php } ?>
Make sure this code is inside the loop and before the endwhile.