【发布时间】:2021-01-25 22:14:37
【问题描述】:
<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 帮助。我想将 2 个元素包装在 <div class="row">. 中,所以基本上是 <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 个项目
【问题讨论】:
标签: php