【发布时间】:2014-04-27 12:28:09
【问题描述】:
所以我正在尝试使用“foreach”来生成每个 div 以具有增量 id,例如 box1 box2 box3 等
这在我的 php 文件中
<div id="box">
<aside class="meta">
</aside>
<section class="post-content">
</section><!--/.post-content -->
</article><!-- /.post -->
生成 4 个:
<div id="box">
<aside class="meta">
</aside>
<section class="post-content">
</section><!--/.post-content -->
</div>
我如何得到(简化形式)
<div id="box1">
</div>
<div id="box2">
</div>
<div id="box3">
</div>
<div id="box4">
</div>
我已经查看了一个较早的问题 How to Assign a unique class to each item returned in a loop?,但我无法理解它在我的上下文中的作用。
更新:
这是我未经编辑的 php 代码:
<div id="box" <?php post_class(); ?>>
<aside class="meta">
<a href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>">
<?php echo get_avatar( get_the_author_meta('email'), '128' ); ?>
</a>
<span class="month"><?php the_time( 'M' ); ?></span>
<span class="day"><?php the_time( 'd' ); ?></span>
<span class="year"><?php the_time( 'o' ); ?></span>
</aside>
<section class="post-content">
<?php
if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] != 'content' ) {
woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] );
}
?>
<header>
<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<?php woo_post_meta(); ?>
</header>
<section class="entry">
<?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'content' ) { the_content( __( 'Continue Reading →', 'woothemes' ) ); } else { the_excerpt(); } ?>
</section>
</section><!--/.post-content -->
</div><!-- /.post -->
【问题讨论】:
-
改用
for循环? -
向我们展示您的 php 代码。
-
任何类型的循环都将使用递增变量并在达到最大值时中断,循环将在中断后停止显示内容,但如果您需要带有附加数字的字符串,请使用连接