如何为 div 生成 ID 或 Class?
How generate ID or Class for div?
有 PHP 代码,当用户点击 the_title(); 然后 the_content会出现
我的问题是如果有无限的the_title();和the_content();,生成 ID 时出现问题,它必须是唯一的。
如何生成 href="#id" 和 id="id"???
<?php
// Start the loop.
while ( have_posts() ) : the_post();
echo '<a href="#id" data-toggle="collapse">';
the_title();
echo '</a>
<div id="id" class="collapse">';
the_content();
echo '</div>';
// End of the loop.
endwhile;
?>
制作一个不断递增的数组。像这样使用它的索引:
<?php echo $new=array('1','2','3','4');?>
foreach($new as $key =>variable){
echo '<a href="#id<?php echo $key+1;?>" data-toggle="collapse">';
the_title();
echo '</a>
<div id="id<?php echo $key+1;?>" class="collapse">';
the_content();
echo '</div>';
}
我试过了。它对我有用。希望你也明白它是如何工作的。
我在 foreach 循环的键值的帮助下增加了 id。
你需要像那个例子那样使用:
$i = 1; // counter start from 1
while ($i <= 10):
echo '<a href="id='.$i.'" data-toggle="collapse"> '.$i.'</a>';
$i++; // increment 1
endwhile;
您的代码示例:
$i = 1; // start counter from 1
while ( have_posts() ) : the_post();
echo '<a href="#'.$i.'" data-toggle="collapse">';
the_title();
echo '</a>
<div id="'.$i.'" class="collapse">';
$i++; // counter
the_content();
echo '</div>';
// End of the loop.
endwhile;
我改变了什么?
只需在 while loop
中添加一个计数器 初始化为 1
有 PHP 代码,当用户点击 the_title(); 然后 the_content会出现
我的问题是如果有无限的the_title();和the_content();,生成 ID 时出现问题,它必须是唯一的。
如何生成 href="#id" 和 id="id"???
<?php
// Start the loop.
while ( have_posts() ) : the_post();
echo '<a href="#id" data-toggle="collapse">';
the_title();
echo '</a>
<div id="id" class="collapse">';
the_content();
echo '</div>';
// End of the loop.
endwhile;
?>
制作一个不断递增的数组。像这样使用它的索引:
<?php echo $new=array('1','2','3','4');?>
foreach($new as $key =>variable){
echo '<a href="#id<?php echo $key+1;?>" data-toggle="collapse">';
the_title();
echo '</a>
<div id="id<?php echo $key+1;?>" class="collapse">';
the_content();
echo '</div>';
}
我试过了。它对我有用。希望你也明白它是如何工作的。 我在 foreach 循环的键值的帮助下增加了 id。
你需要像那个例子那样使用:
$i = 1; // counter start from 1
while ($i <= 10):
echo '<a href="id='.$i.'" data-toggle="collapse"> '.$i.'</a>';
$i++; // increment 1
endwhile;
您的代码示例:
$i = 1; // start counter from 1
while ( have_posts() ) : the_post();
echo '<a href="#'.$i.'" data-toggle="collapse">';
the_title();
echo '</a>
<div id="'.$i.'" class="collapse">';
$i++; // counter
the_content();
echo '</div>';
// End of the loop.
endwhile;
我改变了什么?
只需在 while loop
中添加一个计数器 初始化为 1