最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Template Loop - add switch case php

matteradmin9PV0评论

My plugin template is currently as follows:

 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>

I need to incorporate a switch statement inside the loop but I can't figure out the php syntax.

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

My plugin template is currently as follows:

 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>

I need to incorporate a switch statement inside the loop but I can't figure out the php syntax.

  <?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;
    }
   ?>
Share Improve this question asked Nov 8, 2018 at 10:09 JoaMikaJoaMika 6986 gold badges27 silver badges58 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
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;

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far