douyang2530 2014-10-19 01:12
浏览 66
已采纳

PHP查询结果添加了额外的<li> </ li>

For some reason I am getting an extra <li> </li> at the end of my queried results. How can I prevent this from happening?

<li>
<?php

$counter = 0;
$query = new WP_Query( array( 'post_type' => 'serivces' ) );
while ( $query->have_posts() ) : $query->the_post(); $counter++;
?>
<div class="col-md-4 wp4">

                  <h2 class="text-left"><?php the_title(); ?></h2>
                  <p class="text-left"><?php the_content(); ?></p>
                </div>
<?php 
if ($counter > 0 && $counter % 3 == 0) {
echo '</li><li>';
} 

endwhile;
?>
</li>
  • 写回答

1条回答 默认 最新

  • dongqiang8058 2014-10-19 01:25
    关注

    You could remove the <li> at the top, and the </li> at the bottom, and just some extra if statements at the beginning of the loop and the end of your code, like so:

    <?php
    
    $counter = 0;
    $query = new WP_Query( array( 'post_type' => 'serivces' ) );
    while ( $query->have_posts() ) :
     $query->the_post();
     $counter++;
     if($counter % 3 == 1) { echo '<li>'; }
    ?>
     <div class="col-md-4 wp4">
       <h2 class="text-left"><?php the_title(); ?></h2>
       <p class="text-left"><?php the_content(); ?></p>
      </div>
    <?php 
     if ($counter > 0 && $counter % 3 == 0) {
      echo '</li>';
     } 
    endwhile;
    
    if($counter % 3 != 0) { echo '</li>'; }
    ?>
    

    The if statement at the bottom only executes if the last if statement inside the while loop didn't occur on an iteration that was a multiple of 3, to ensure that the list element always gets closed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题