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;
Related
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!
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
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; ?>
So what I'm trying to do is select all the distinct months from my database and then print them in a list. That, I can accomplish. The problem lies in the fact that I need my list to be two column. The way that I achieve this with CSS is by using 2 different div's "left" and "right" which are floated next to each other. This poses a problem with PHP because it needs to echo a div close and a new div open after it echoes the sixth month. Then it needs to start again from where it left off and finish. I can't just list all of the months in the HTML, either because I don't want it to list a month if I don't have any records in the DB for that month, yet. Any ideas? I hope I was clear enough!
Thanks!
-williamg
Something like this should work (the basic idea being to just keep a count of the months an increment it as you loop through them):
<div class="left">
<?php
$x = 1;
foreach($months as $month) {
# switch to the right div on the 7th month
if ($x == 7) {
echo '</div><div class="right">';
}
echo "<div class=\"row\">{$month}</div>";
# increment x for each row
$x++;
}
</div>
<?php
$numberOfMonths = count($months);
$halfwayPoint = ceil($numberOfMonths / 2);
echo "<div class=\"left\">";
for($i=0; $i<$halfwayPoint; $i++){
echo $months[$i] . "<br />";
}
echo "</div><div class=\"right\">";
for($i=$halfwayPoint; $i<$numberOfMonths; $i++){
echo $months[$i] . "<br />";
}
echo "</div>";
?>
Rant: on
When displaying tabular data, use table instead of floating div. It will make sense when viewing the page with css disabled. If you use floated div, then you data will displayed all way down. Not all table usage is bad. People often hate table so much, so using floated div. Table only bad when used for page layout.
Rant: off
When I need to have certain content displayed with some open, close, and in-between extra character, I will make use of implode. This is the example:
$data = array('column 1', 'column 2');
$output = '<div>'.implode('</div><div>', $data).'</div>';
//result: <div>column 1</div><div>column 2</div>
You can extends this to almost anything. Array and implode is the power that php have for many years. You will never needed any if to check if it last element, then insert the closing character, or check if it first element, then insert opening character, or print the additional character between elements.
Hope this help.
Update:
My bad for misread the main problems asked. Sorry for the rant ;)
Here is my code to make a data displayed in 2 column:
//for example, I use array. This should be a result from database
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
//should be 12 month, but this case there are only 9 of it
for ( $i = 0; $i <= 5; $i++)
{
//here I do a half loop, since there a fixed number of data and the item for first column
$output = '<div class="left">'.$data[$i].'</div>';
if ( isset($data[$i+6] )
{
$output = '<div class="right">'.$data[$i+6].'</div>';
}
echo $output."\n";
}
//the result should be
//<div class="left">1</div><div class="right">7</div>
//<div class="left">2</div><div class="right">8</div>
//<div class="left">3</div><div class="right">9</div>
//<div class="left">4</div>
//<div class="left">5</div>
//<div class="left">6</div>
Other solution is using CSS to format the output, so you just put the div top to down, then the css make the parent container only fit the 6 item vertically, and put the rest to the right of existing content. I don't know much about it, since it usually provided by fellow css designer or my client.
Example assumes you have an array of objects.
<div style="width:150px; float:left;">
<ul>
<?php
$c = count($categories);
$s = ($c / 3); // change 3 to the number of columns you want to have.
$i=1;
foreach($categories as $category)
{
echo '<li>' . $category->CategoryLabel . '</a></li>';
if($i != 0 && $i % $s == 0)
{
?>
</ul>
</div>
<div style="width:150px; float:left;">
<ul>
<?php
}
$i++;
}
?>
</ul>
</div>