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!
Related
Hi I'm a newbie with PHP and this site, so please be nice :)
I'm currently having trouble working the below PHP foreach code out as I'm trying to echo all images in a HTML table 3 column but it echo's with 2 only.
UPDATE: I've managed to fix some issues thanks to the comments guy's, thank you. However, I', now experiencing another issue which is confusing.
Basically, If I have one picture in a folder, it will echo that one picture, but If I put two pictures there, it echo's out with 4, 1 first picture echo's with 2 and the second is with 2 as well. Basically showing 4 images even though I have 2 images in that folder. I can't seem to fix this..
Here's the code:
<?php
// get images
$images = glob($imagedir.'/' . "*.png");
$i = 0;
echo'<table><tr>';
foreach($images as $image)
{
$i++;
echo '<td><img src="'.$image.'" height="200"></td>';
if($i == 3)
{
echo '</tr><tr>';
$i = 0;
}
}
echo '</tr></table>';
?>
Thanks in Advance
You're code for rendering the HTML is just fine.
If you have duplicates, the content of your imagedir must be wrong.
A few remarks:
glob($imagedir.'/' . "*.png"); also include directories which names end as .png.
Depending on the amount of images, the last table row will not be completely filled with table cells.
It's good practice not using the php closing tag ?> at the end a the php file.
I've altered you code to avoid above to problems.
I'm sure there more/other ways to do this, but this came in mind first.
<?php
$imagedir = 'images';
//Get *.png files only
$images = array_filter(glob("$imagedir/*.png"), 'is_file');
//Make image array divisble by 3 (columns)
while (count($images) % 3 != 0) {
$images[] = '';
}
echo'<table><tr>';
for ($i = 1; $i <= count($images); $i++) {
//Render TD if $image is not empty
if ($images[$i-1] != '' != '') {
echo '<td><img src="' . $images[$i-1] . '">', "<br>Image $i</td>";
}
//Close table row after 3 TD's
if($i % 3 == 0)
{
echo '</tr><tr>';
}
}
echo '</tr></table>';
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
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;
Ok here is my new issue, and a complete description of what I am trying to accomplish. I used the suggestions you gave me and it worked fine, but when I changed the ORDER by information to a different field and the if statement, it went crazy.
Here is what I need, I have two columns on a page, when the array is processed I need it to display results filling each column left to right top to bottom. There is a division line between each item
<div class="centerBoxContentsFeatured centeredContent back vLine " style="width:50%;">
and one under each item
<div class="product-col" >.
If the item is the 3rd, 5th, 7th and so on… it will be on the left and does not need the “vLine “ div before it and if it is the last item(s) it does not need the “product-col” div under it as the last 2 items do not have to have the bottom divider, but if it is only two items on the whole page the bottom divider can stay. What am I doing wrong?
<?php
$count_temp = count ($productid);
for ($i = 0; $i < $count_temp; ++$i) {
$artist = get_the_title();
if (($productartist[$i] == $artist ) && $categoryID[$i] ==1 ) {
?>
//Content//
<?php if (!($i === 0) && !($i % 2) &&
($productid[$i] == 1) &&
( $i === (count ($productid) - 1))) {
echo'<div class="product-col-none" >';
}
else { echo '<div class="product-col" >';}
?>
//Content//
<? if !( $i === (count ($productid) - 1))
{
echo '<div class="centerBoxContentsFeatured centeredContent back vLine " '.
'style="width:50%;">';
}
else {
echo '<div class="centerBoxContentsFeatured centeredContent back " '.
'style="width:50%;">';}
?>
You should write here about your css file code, you are making things complicated and unclear with your html embedded php code.
If you just want a different style for your odd numbered (3,5,7..) items and a different style for your last item then I'll recommend to use css pseudo classes
You can use css pseudo class last-child for your last item.
li:last-child
{
// Your specific style for last item goes here
}
You can also use css pseudo class nth-child(odd) for alternating odd numbered items
li:nth-child(odd)
{
// Your specific style for odd items goes here
}
I hope this is what you are looking for, Why to give a server side load when you can set things done at client end.
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; ?>