在PHP(Wordpress)循环中,您如何生成一系列唯一编号?
问题描述:
如果我在一个循环生成一个列表,像:在PHP(Wordpress)循环中,您如何生成一系列唯一编号?
<?php if (have_posts()) while (have_posts()) : the_post(); ?>
<div id="slide">
<ul>
<li class="slide-<?php //how can I generate an integer here ?>">
<a href="#">stuff</a></li>
</ul>
</div>
我怎么可能一个序列号附加到每个这些类的,和/或那些的HREF?
所以在这个例子幻灯片1,幻灯片2,等等。
答
<?php
if (have_posts()) :
$slideNumber = 1;
while (have_posts()) : the_post(); ?>
<div id="slide">
<ul>
<li class="slide-<?php echo $slideNumber++; ?>">
<a href="#">stuff</a></li>
</ul>
</div>
<?php
endwhile;
endif; ?>
答
我在考虑,你会放一些幻灯片在同一个页面。所以我会把所有的HTML代码放在PHP代码中。类似的东西:
<?php
while ($count <= $total) {
echo "<div id=\"slide\">";
echo "<ul>";
echo "<li class=\"slide-".$count."\">";
echo "<a href=\"#\">stuff</a></li>";
echo "</ul>";
echo "</div>";
count++;
}
?>
我没有检查代码,但它只是一个想法。
祝你好运。
太棒了!谢谢。 – Zac 2010-09-12 07:36:34