<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>
你好堆叠器!
我需要一些关于如何包装循环元素的php帮助。我想在<div class="row">.
中包含2个元素,所以基本上<row> <item> <item> </row>
我已经尝试了一些模数,你可以看到,一些if语句仍然存在。我将i设置为0,并且当1%2 = 0时尝试放置<div class="row">
但是没有找到关于如何正确关闭标签的解决方案(应该在第二项之后关闭)
有没有机会你可以帮助我作为新手php黑客?
编辑:
<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>
这只包装了我所有的循环项目,我希望div class = row只包装每2个项目
答案 0 :(得分:10)
你快到了:
//Build custom items
echo "<row>";
$i = 0;
foreach($children as $child) {
echo "item ";
$i++;
if ($i % 2 == 0 && $i != count($children)) {
echo "</row><row>";
}
}
echo "</row>"
答案 1 :(得分:6)
或者这个:
<?php
$i=0;
foreach($children as $child){
++$i;
if($i==1){
echo "<row>";
echo "<item>$child</item>";
}
if($i==2){
echo "<item>$child</item>";
echo "</row>"
$i=0;
}
}
[UPDATE]
这让我感到困惑:一个奇怪的孩子可能会导致一排没有关闭标签。 虽然大多数浏览器只会在渲染上添加标记,但您根本没有任何问题,但这仍然不是100%正确。
在奇怪的孩子数量上,你会想要在foreach循环之后检查和关闭一行,如下所示:
if($i==1){
echo "</row>";
}
如果循环后$ i == 1,那么这是一个奇数的子项,并且必须关闭该行。
[/ UPDATE]
答案 2 :(得分:1)
您应该使用for
循环而不是foreach
循环,如下所示:
for($i = 0; $i < count($children); $i+=2) {
$child1 = $children[$i];
$child2 = $children[$i+1];
// print both
}
如果你可能有一些奇数的孩子,你必须在打印之前检查$i+1 < count($children)
。
答案 3 :(得分:0)
试试这个
$i = 1;
//Build custom items
foreach($children as $child){
if($i>2){
$i =1;
}
if ($i==2) {
//close row
}
$i++;
}
答案 4 :(得分:0)
这是在Wordpress中,但可以理解,它将为您提供帮助。
<?php $query = new WP_Query( array( 'post_type' => 'slides', 'order'=> 'DESC', 'post_status' => 'publish', 'posts_per_page' => -1) );
$posts = $query->posts;
$numOfCols = 2;
$rowCount = 0;
if(!empty($posts)){ ?>
<div class="carousel-item <?php echo ($numOfCols-1==1)?'active':''; ?>">
<div class="row">
<?php foreach ($posts as $post) { ?>
<div class="col-md-6 pt-4 pb-0 " >
<h6 class="mb-2 text-uppercase"><b><a href="<?php echo get_permalink( $post->ID); ?>" target="_blank"><?php echo $post->post_title; ?></a></b></h6>
<span><?php echo get_the_excerpt($post->ID); ?></span><span class="float-right"><a href="<?php echo get_permalink( $post->ID); ?>" target="_blank"><i _ngcontent-ttx-c19="" class="material-icons icon-image-preview" style="position: relative; top: 7px;">arrow_forward</i></a></span>
</div>
<?php
$rowCount++;
if($rowCount % $numOfCols == 0 && $rowCount != count($posts)) echo '</div></div><div class="carousel-item "><div class="row"> ';
} ?>
</div>
</div>
<?php } ?>
答案 5 :(得分:0)
# Process every second item starting with the first one [0].
foreach ($array as $key => $value) {
if (($key - 1) % 2 === 0) {
continue;
}
# Do something here.
}