因此,我试图使用“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 -->发布于 2014-04-27 12:37:30
假设您在玩Wordpress,文章将在while loop.中呈现。
可能您的post模板是由另一个文件通过
get_template_part#1使用css
使用css应用自定义样式表。将自定义类应用于您的div,如.customdiv,然后使用div.customdiv:nth-child(x)对它们进行样式设置。不需要作进一步的修改。
#2将单个模板移动到主php文件中
您可以将模板部件复制到主文件并替换
get_template_part(..)它的内容。这不是一个好主意,因为您必须在使用此模板的所有主文件中这样做。将来要修改它们就更难了。
#3糟糕但工作(?)
//可能不起作用,因为我已经很久没有使用全局词了。我强烈建议你不要用那种方法,这是FYI
只需在主php文件中执行以下操作:
//before starting the while loop
$divId = 0;在您的文件中,模板在同一开头:
global $divId;然后,只需替换以下内容:
id="box"使用
id="box<?php echo ++$divId; ?>"发布于 2014-04-27 12:35:43
使用for循环来尝试这个
for ($i = 1; $i <= 4; $i++) {
?>
<div id = "box<?php echo $i ?>"></div >
<?php
}发布于 2014-04-27 12:36:19
可以尝试(简化):
<?php
$i = 0;
foreach ($data as $key => $value)
{
$i++;
echo '<div id="box' . $i . '">';
// content
echo '</div>';
}https://stackoverflow.com/questions/23323390
复制相似问题