0
<?php 
    $freeadvice=new WP_Query('category_name=freeadvice&showposts=10');
    while($freeadvice->have_posts() ): $freeadvice->the_post();
?>
    <li class="event">
        <input type="radio" name="tl-group" />
        ....
</li>
<?php endwhile;?>

这是我当前的循环代码,但我试图在第一篇文章中创建它,即最近的一篇文章应该将内部列表项的第一行作为

<input type="radio" name="tl-group" checked/>

现在每次我添加一个新帖子时,我都需要使用这个属性添加第一个孩子,这可能是使用 php 还是可能是 javascript

4

2 回答 2

2

你可以使用一个标志

<?php 
    $flag = true;
    $freeadvice=new WP_Query('category_name=freeadvice&showposts=10');
    while($freeadvice->have_posts() ): $freeadvice->the_post();
?>
    <li class="event">
  <input type="radio" name="tl-group" <?php if ($flag) { echo "checked"; $flag = false; } ?>/>
        ....
</li>
<?php endwhile;?>
于 2013-11-01T06:28:37.173 回答
0

至于我的理解,你希望第一个 Li 项目中的单选按钮具有选中的属性。我对吗?

我要做的是在 while 循环之前用零初始化一个变量,并且仅在第一个实例中我会提供选中的属性。剩下的我不会。例子:

<?php 
    $freeadvice=new WP_Query('category_name=freeadvice&showposts=10');\
    $x=0; //initializing variable
    while($freeadvice->have_posts() ): $freeadvice->the_post();
        if($x==0) //If block is executed only for the first instance
        {
?>
            <li class="event">
                <input type="radio" name="tl-group" checked/> //providing checked attribute
                ....
            </li>
    <?php
        }else{ //else block is executed for all other instances
    ?>
            <li class="event">
                <input type="radio" name="tl-group" />
                ....
            </li>
    <?php
        }
        $x=$x+1; //incrementing x value
    ?>
<?php endwhile;?>

所以这样只有当$x变量为0时,也就是第一次,if块才会被执行,单选按钮才会得到属性checked。然后$x递增,并且每隔一次执行 else 块。

$x在开始 while 循环之前,每次都声明为 0 很重要。因此在 while 循环之前进行初始化。

希望能帮助到你..

于 2013-11-01T06:44:44.087 回答