gpt4 book ai didi

html - 如何将每 3 个 div 的内容包装在一个 div 中?

转载 作者:行者123 更新时间:2023-11-28 04:39:46 26 4
gpt4 key购买 nike

我需要将类“loopcontent”的每三个 div 包装在类“threeacross”的新 div 中。我是 WordPress 的新手,还不是 PHP 专家,但我正在学习。

这是我的循环:

        <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="loopcontent">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else {
// the current post lacks a thumbnail
}
?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_excerpt(); ?>
</div>

<?php endwhile; else : ?>

<div class="loopcontent">
<h1>Page Not Found</h1>
<p>Looks like the page you're looking for isn't here anymore. Try browsing the <a href="">categories</a>, <a href="">archives</a>, or using the search box below.</p>
<?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>

<?php endif; ?>

最佳答案

像这样的东西应该可以工作。这会保持运行计数并每三个帖子放置一次 div。让我知道这是否有帮助。如果没有,让我知道它输出了什么,我可以调整它:

<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="threeacross">' : ''; //Create open wrapper if count is divisible by 3
$close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
echo $close.$open;
?>
<div class="loopcontent">
<?php
if(has_post_thumbnail()) the_post_thumbnail();
else{
// the current post lacks a thumbnail
}
?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_excerpt(); ?>
</div>
<?php endfor; else : ?>
<div class="loopcontent">
<h1>Page Not Found</h1>
<p>Looks like the page you're looking for isn't here anymore. Try browsing the <a href="">categories</a>, <a href="">archives</a>, or using the search box below.</p>
<?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>

关于html - 如何将每 3 个 div 的内容包装在一个 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10507821/

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