2013-11-01 41 views
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;?> 

这是我的当前代码为我的循环,但我想创建在第一篇文章,这是最近的一篇文章应该有作为wordpress,如何更改一个循环中的第一个职位的内容

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

我每次添加一个新的岗位,我需要与此属性添加的第一个孩子的时候,内部列表项的第一行是这可能使用PHP或可能的JavaScript

回答

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;?> 
+0

哦,是没想到这个谢谢 – DeadMan

+0

没有问题。请标记答案为接受,如果这能解决您的问题:) – zoranc

+0

是的,我这样做,但它说不能在4分钟前标记,谢谢btw – DeadMan

0

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

我会做的是在while循环之前用零初始化一个变量,并且只为第一个实例提供checked属性。剩下的我都不会。实施例:因此,在仅当$x变量是0这种方式

<?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;?> 

,即仅在第一次时,如果执行块和单选按钮会得到一个属性checked。那么$x会递增,并且每隔一次执行else块。

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

希望它可以帮助..

+0

一个非常有效的代码是由zoranc给出。这只是一个漫长的过程,但容易理解。 –

相关问题