【发布时间】:2015-09-28 22:16:24
【问题描述】:
我在网站上使用 WordPress。 我有下面的粘贴循环,我想为每个输出 id 名称添加 +1。
所以当前 ID 是 id="plan1_price" 在每个循环之后,我希望它变成 id="plan2_price"、id="plan3_price" 等...
<table class="
<?php
$best = rwmb_meta( 'tb_table1_bestcss' );
if (!empty($best)){ echo "highlight-best-" . rwmb_meta( 'tb_table1_bestcss' ) . " "; }
$popular = rwmb_meta( 'tb_table1_popularcss' );
if (!empty($popular)){ echo "highlight-popular-" . rwmb_meta( 'tb_table1_popularcss' ); }
?>
" border="0" cellspacing="0" cellpadding="0">
<tr>
<?php
$tableheading = rwmb_meta( 'tb_table1_heading', 'type=text' );
foreach ( $tableheading as $index => $heading ) {
?>
<th scope="col">
<h3><?php echo $heading; ?></h3>
<p class="sub-heading">
<?php
$tablesub = rwmb_meta( 'tb_table1_sub_heading' );
if (!empty($tablesub)) {
$tablesubheading = rwmb_meta( 'tb_table1_sub_heading', 'type=text' );
echo $tablesubheading[$index];
}
?>
</p>
<!--
THIS IS THE ID i WOULD LIKE TO ADD +1 TO -- id="plan1_price"
-->
<div id="plan1_price" class="price">
<sup>$</sup>
<span class="amount">
<?php
$tableprice = rwmb_meta( 'tb_table1_price_heading' );
if (!empty($tableprice)) {
$tablepriceheading = rwmb_meta( 'tb_table1_price_heading', 'type=text' );
echo $tablepriceheading[$index];
}
?>
</span>
<small class="per-period">/mo</small>
</div>
</th>
<?php
}
?>
</tr>
</table>
【问题讨论】:
-
你的意思是像
id="plan1_price_<?= $plan1_price_id++ ?>"这样的吗? -
@FoxRider 你不应该建议其他用户使用 PHP 短标签,并非所有服务器/设置都会启用此功能
-
@DannyBroadbent
<?=已保存以供使用,并且“无论 short_open_tag ini 设置如何,始终可用”;见php.net/basic-syntax.phptags -
@FoxRider 自 PHP 5.4.0 起,= 始终可用。默认情况下,旧版本的 PHP(许多用户仍在使用)将不提供此功能。不是每个人都能掌握最新的 PHP(有些人拥有庞大的系统,可能需要几个月的时间来改变,有些人更喜欢旧的 PHP 代码,因为他们没有适应新的变化)。这就是我的意思。很高兴为新的更新/更改提供建议,但除非您知道他们自己在使用它,否则我不会使用它。
-
@DannyBroadbent 是的,你是对的,我绝对支持你的方法。我只是没有注意到它只对 PHP >= 5.4.0 有效。我的错。 :( 幸运的是,cmets 不能拒绝投票。^^