Wordpress, display item in specific row - php

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.

Related

Break the loop into half, if the loop count is even

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

Give a unique id to the first three elements and repeat for next three

I have this line of code which generates posts from a certain post type:
<div class="row">
<?php
$args = array(
'post_type' => 'cases',
'posts_per_page' => 3,
offset => 2
);
$the_query1 = new WP_Query( $args );
if ( $the_query1 -> have_posts() ):
while ( $the_query1->have_posts() ): $the_query1->the_post();
?>
<div class="col-sm-4">
<div class="case-container">
<div class="case-info-triple">
<h4 id="case-title"><?php the_title(); ?></h4>
<p id="case-type"><?php the_field('case_type') ?></p>
<p id="case-desc"><?php $caseteaser = get_field('case_content', false, false); echo substr($caseteaser, 0, 200); ?></p>
<img src="Diensten - zeo9-arrow.png" />Bekijk deze case
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata();?>
<?php endif;?>
</div>
That generates this:
However, I would like the every three 'case-info-triple' elements to have their own unique id. For example, <div class="case-info-triple-1">, <div class="case-info-triple-2">, <div class="case-info-triple-3"> and then again, <div class="case-info-triple-1">, <div class="case-info-triple-2"> etc etc.
See corresponding source:
We show for now 3 posts per page, but we will change it to 9 in the future. Basically, every first, second and third info element has its own color and I need them to have a unique id so I can style them with css.
How to approach this?
Before your while loop, initialize a variable:
$cnt = 0;
Then inside your while loop you can put this:
$classID = $cnt++%3;
to increment the counter and then modulus by 3. You can adjust this for any number of columns. Using 3 as your modulus will produce a number between 0 and 2. Then use the classID here:
<div class="case-info-triple<?php echo '-'.$classID; ?>">
Or you can skip the $cnt variable and do the increment at the same time that you echo the classID, like this:
$classID = 0;
while($classID <= 5) { ?>
<div class="case-info-triple<?php echo '-'.$classID++%3; ?>">text</div>
<?php }
There are many option but you use a counter variable and add that in class name to make it unique ,for example in your code you can do like this:-
<div class="row">
<?php
$args = array(
'post_type' => 'cases',
'posts_per_page' => 3,
offset => 2
);
$the_query1 = new WP_Query( $args );
if ( $the_query1 -> have_posts() ):
$id = 1;
while ( $the_query1->have_posts() ): $the_query1->the_post();
$infoClassName = "case-info-triple-"+$id;
?>
<div class="col-sm-4">
<div class="<?php echo $infoClassName; ?>">
<div class="case-info-triple">
<h4 id="case-title"><?php the_title(); ?></h4>
<p id="case-type"><?php the_field('case_type') ?></p>
<p id="case-desc"><?php $caseteaser = get_field('case_content', false, false); echo substr($caseteaser, 0, 200); ?></p>
<img src="Diensten - zeo9-arrow.png" />Bekijk deze case
</div>
</div>
</div>
<?php $id++; ?>
<?php endwhile; wp_reset_postdata();?>
<?php endif;?>
Can you add arguments to your array that indicates rows (3 rows with 3 posts)?
$args = array(
'post_type' => 'cases',
'posts_per_row' => 3,
'rows_per_page' => 3,
Then move your html around the script so you can insert an if... elseif... else section to automate naming your <div class="case-info-triple-1">, -2, or -3, based on which row of posts it is in.

Add a unique class to last row of a bootstrap 4 wordpress theme

I've been researching ways to add a unique class to the last row of a bootstrap 4 grid layout. I found many examples of how to identify the last post in a wordpress query, but identifying the start of the last row so I could add a unique class was harder to locate.
In the end, I did some (in retrospect) basic math to calculate which loop in a while statement contained the first post of the last row, and inserted the desired 'last' class at that point.
This code is working ($project_columns is an integer between 1 and 12) but I was wondering if my solution has any performance issues or could have been done "better".
<!-- Project Thumbnails -->
<?php if ( $show_project_thumbnails === true ) : ?>
<?php
$args = array(
'post_type' => 'projects',
'orderby' => $project_orderby,
'order' => $project_order,
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts ()) :
$counter = 0;
$post_count = $the_query->post_count;
$total_rows = (ceil) ( $post_count / $project_columns );
$first_post_last_row = ( $total_rows - 1 ) * $project_columns;
?>
<div id="projects">
<div class="row content-area bg-light projects">
<div class="col projects-title">
<h1 class="text-center display-3">Recent Projects.</h1>
<hr class="separator"></span>
</div><!-- .col end -->
</div><!-- .row end -->
<?php
while ( $the_query->have_posts ()) : $the_query->the_post();
if ($counter % $project_columns == 0) :
echo $counter > 0 ? "</div>" : ""; // close div if it's not the first
echo $counter == $first_post_last_row ? "<div class=\"row content-area bg-light last\">" : "<div class=\"row content-area bg-light\">";
endif;
?>
<div class="col-md projects-thumbnails">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"">
<?php
the_post_thumbnail();
echo $items;
?>
</a>
</div><!-- .col-md end -->
<?php $counter++;
endwhile;
endif;
wp_reset_postdata();
?>
</div><!-- .row end -->
</div><!-- #projects end -->
<?php endif; ?>
<!-- /Project Thumbnails -->
I think that the easiest way to select the last row is to use CSS :last-child selector.
you can access last row by something like that
<?php
$last_key = count($array) - 1;
if(key($array) == $last_key){
// do something
}
?>
or if they key is not number
<?php
$last_key = count($array) - 1;
$last_row = $array[$last_key];
if(key($array) == $last_row){
// do something
}
?>

Creating a loop within a loop in Wordpress to display 12 posts in groups of two contained in separate divs

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

How do you limit the amount of posts shown in a while loop?

So, I've been looking all over and tried different methods, but I can't seem to get to limit a number of posts shown. Can anyone help?
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="portfolio-item <?php the_field('category'); ?> <?php the_field('thumbnail_size'); ?>">
<div class="portfolio-wrapper">
<div class="portfolio-img-wrap" data-background="<?php the_field('image'); ?>"></div>
<div class="portfolio-overlay"></div>
<div class="portfolio-caption">
<h6 class="portfolio-title"><?php the_title(); ?></h6><span class="portfolio-subtitle"><?php the_field('category'); ?></span>
</div>
</div>
<a class="portfolio-link" href="portfolio-single.html"></a>
</div>
<?php endwhile; ?>
set a if statement its not so hard:)
then just save then info in get when to start and when to break then u have it:D
You could add a count variable and break out of loop when you want.
See the code below
$count = 0;
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
$count++;
<div class="portfolio-item <?php the_field('category'); ?> <?php the_field('thumbnail_size'); ?>">
<div class="portfolio-wrapper">
<div class="portfolio-img-wrap" data-background="<?php the_field('image'); ?>"></div>
<div class="portfolio-overlay"></div>
<div class="portfolio-caption">
<h6 class="portfolio-title"><?php the_title(); ?></h6><span class="portfolio-subtitle"><?php the_field('category'); ?></span>
</div>
</div>
<a class="portfolio-link" href="portfolio-single.html"></a>
</div>
if($count == 10){
break;
}
<?php endwhile; ?>
Her is something clearly wrong.
A while loop shows all results that you got. If you want to limit that you should change the statement that fetches the posts not the while loop.
Otherwise you will fetch x posts and only output for example 10 ... That is an performance factor.
If you want to change the fetch result you have to modify the wp query (as others mentioned).
But in your example there is no custom query. Its the default one... So you can change the amount of posts per page in your backend under settings. Otherwise you should use a custom query and not the default one:
$myposts = get_posts( array( 'showposts'=>10 ) );
if( $myposts ):
global $post; foreach( $myposts as $post ): setup_postdata( $post );
// Your output like the_title() etc
endforeach:
endif;

Categories