gpt4 book ai didi

Php foreach 循环包装每 2 个项目

转载 作者:可可西里 更新时间:2023-11-01 13:07:16 24 4
gpt4 key购买 nike

<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,并试图将 <div class="row">当 1 % 2 = 0 但没有找到关于如何正确关闭标签的解决方案(应该在第二项之后关闭)你有没有机会帮助我这个新手 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 个项目

最佳答案

你快到了:

    //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>"

关于Php foreach 循环包装每 2 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com