Php foreach wraps every 2 elements
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) : get_page_children( $post->ID, $all_wp_pages );
$i = 0;
//Build custom items
foreach($children as $child){
$i++;
/*
if (i % 2 == 0) { ?>
<?php
} */
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail( $child->ID, 'full' ); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field( 'puff_lanktext', $child->ID );
if (get_field( "popup_eller_lank", $child->ID ) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class ="linktopage open-popup" href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
</fieldset>
<?php
} elseif (get_field( "popup_eller_lank", $child->ID ) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class ="linktopage" href="<?php echo get_field( "puff_lank", $child->ID ); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
<?php
} else { }
?>
</div>
</div>
<?php } ?>
</div>
Hello stackers!
I need some php help on how to wrap elements with loops. I want to wrap 2 items in <div class="row">.
So basically<row> <item> <item> </row>
I tried with some module, as you can see, some if statements still exist. I set i as 0 and tried to put <div class="row">
when 1% 2 = 0 but couldn't find a solution on how to close tags correctly (should be closed after second element) Any chance you could help me as a php hacker newbie ?
EDIT:
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) : get_page_children( $post->ID, $all_wp_pages );
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach($children as $child){
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail( $child->ID, 'full' ); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field( 'puff_lanktext', $child->ID );
if (get_field( "popup_eller_lank", $child->ID ) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class ="linktopage open-popup" href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
</fieldset>
<?php
} elseif (get_field( "popup_eller_lank", $child->ID ) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class ="linktopage" href="<?php echo get_field( "puff_lank", $child->ID ); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
<?php
$i++;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
} else { }
?>
</div>
</div>
<?php } ?>
</div>
</div>
This only wraps all my looped elements, I want the div class = row to wrap only every 2 elements
source to share
You should use a loop for
instead of a loop foreach
, for example:
for($i = 0; $i < count($children); $i+=2) {
$child1 = $children[$i];
$child2 = $children[$i+1];
// print both
}
if you may have an odd number of children, you need to check if there is one $i+1 < count($children)
before printing it.
source to share