模板循环-添加开关柜php

时间:2018-11-08 作者:JoaMika

我的插件模板当前如下:

 if ( $query->have_posts() )
 {
    ?>
 <ul id="list-con">
    <?php
    while ($query->have_posts())
    {
        $query->the_post();

        ?>

        <li>
        <a id="floimg" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail("mini-me");} ?><span class="flotnm"><?php the_field(\'fl_name\'); ?></span></a>
        </li>

        <?php
    }
    ?>
 </ul>
我需要在循环中加入switch语句,但我无法理解php语法。

  <?php $curtype = get_post_type( $post->ID ); switch($curtype){
      case "firstcase":
        return <li> code as defined above
        break;
   case "secondcase":
        return some other <li> code as defined above
        break;
    }
   ?>

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
if ( $query->have_posts() ) : ?>
    <ul id="list-con">
    <?php
        while ($query->have_posts()) :
            $query->the_post();
    ?>
        <?php
             switch ( get_post_type( $post->ID ) ) {
                 case "firstcase":
                     ?>
                     <li>code as defined above</li>
                     <?php
                     break;

                 case "secondcase":
                     ?>
                     <li>another code as defined above</li>
                     <?php
                     break;
             }
        ?>
    <?php
        endwhile; 
    ?>
    </ul>
<?php endif;
结束