【问题标题】:How to echo unique incremental id to div in foreach loop?如何在 foreach 循环中将唯一的增量 id 回显到 div?
【发布时间】: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 &rarr;', 'woothemes' ) ); } else { the_excerpt(); } ?>
        </section>


    </section><!--/.post-content -->

</div><!-- /.post -->

【问题讨论】:

  • 改用for 循环?
  • 向我们展示您的 php 代码。
  • 任何类型的循环都将使用递增变量并在达到最大值时中断,循环将在中断后停止显示内容,但如果您需要带有附加数字的字符串,请使用连接

标签: php arrays loops foreach


【解决方案1】:

假设您正在使用 Wordpress,帖子将在 while loop. 中呈现

您的帖子模板可能是通过使用另一个文件获取的

get_template_part

#1 使用 css

使用 css 应用自定义样式表。将自定义类应用于您的 div,例如 .customdiv,然后使用 div.customdiv:nth-child(x) 设置它们的样式。无需进一步修改。

#2 将您的单个模板移动到主 php 文件中

您可以将模板部分复制到主文件并替换

get_template_part(..)

及其内容。这不是一个好主意,因为您必须在使用此模板的所有主文件中执行此操作。将来修改它们会更难。

#3 糟糕但有效(?)

// 可能不起作用,因为我已经很久没有使用全局变量了。我强烈建议你不要使用这种方法,这里仅供参考

只需在您的主 php 文件中执行以下操作:

//before starting the while loop
$divId = 0;

并且在您的文件中以模板开头:

global $divId;

然后,只需替换以下内容:

id="box"

id="box<?php echo ++$divId; ?>"

【讨论】:

  • 我试过这个。但所有 id = box1 而不是递增。知道为什么吗?
  • @mfk 好的,我知道为什么它不起作用。循环可能与整个帖子模板位于不同的文件中。
  • 最后一行中的 $i 应该是.. $divID 吗?
  • 是的,对不起,我的错误。
  • #3 就像一个魅力。谢谢!但是你有理由说它很糟糕吗?
【解决方案2】:

使用for 循环试试这个

for ($i = 1; $i <= 4; $i++) {
    ?>

    <div id = "box<?php echo $i ?>"></div >

<?php
}

【讨论】:

    【解决方案3】:

    也许试试(简化):

    <?php
    $i = 0;
    foreach ($data as $key => $value)
    {
        $i++;
        echo '<div id="box' . $i . '">';
        // content
        echo '</div>';
    }
    

    【讨论】:

    • 错字,应该是 $key => $value ;)
    【解决方案4】:
    <?php 
    $counter = 1; 
    foreach($results as $result){
    ?>
    <div id="box<?php echo $counter; ?>">
    </div>
    <?php $counter++;
    } ?>
    

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 2023-03-20
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多