dqp4933 2016-10-19 20:57
浏览 61
已采纳

PHP elseif语句不适用于wordpress中的高级自定义字段

I made a hybrid grid with content that gets populated through Advanced Custom Fields. Everything works ok, except that when I set up an if/else statement to go with the checkbox I set up in the ACF in WordPress admin, it causes a 500 error. This is the main block of code that has the if/else statement:

    <div class="watcheroos">
    <?php

    if(count($postslist) > 0) { ?>

    <ul class="list-unstyled">

    <?php 

        $clearfix_counter = 0;

        foreach ($new_list as $index => $post) { 

        $bkg_id =  get_post_thumbnail_id( $post->ID );
        $bkg_src = wp_get_attachment_image_src( $bkg_id, 'medium' );
        $bkg_src_lg = wp_get_attachment_image_src( $bkg_id, 'large' );  

        $network = get_post_meta($post->ID,'network',true);
        $timeofday = get_post_meta($post->ID,'time_of_day',true);
        $date = get_post_meta($post->ID,'date',true);
        $producer = get_post_meta($post->ID,'producer',true);


        if($index %8 == 0 || $index %9 == 0 || $index == 1 ) { ?>

            <?php if ($index == 0) { ?>

                <li class="watcheroo first <?php if($addClass){echo 'widewidth'; } ?>" id="<?php echo sanitize_title($post->post_title); ?>">

                    <div class="content" <?php if($bkg_src_lg[0]){ echo 'style="background-image: url(' . $bkg_src_lg[0] . ');"'; } ?> >

                    </div>
                    <div class="featured-info">
                        <h4><?php echo $post->post_title; ?></h4>
                        <div class="info">
                            <div class="network"><strong>Network:</strong> <?php echo $network; ?></div>
                            <div class="timeofday"><strong>Time-of-day:</strong> <?php echo $timeofday; ?></div>
                            <div class="date"><strong>Date:</strong> <?php echo $date; ?></div>
                            <div class="producer"><strong>Produced by:</strong> <?php echo $producer; ?></div>
                            <?php echo apply_filters('the_content', $post->post_content); ?>
                            <?php 
                                $terms = get_the_terms($post->ID,'watcheroo_production_credits');
                                if($terms) {
                                    echo '<ul class="credits list-unstyled list-inline">'; 
                                    foreach ($terms as $term) {
                                        echo '<li>' . $term->name . '</li>';
                                    }
                                    echo '</ul>';
                                }
                            ?>
                        </div>
                    </div>

                </li>



            <?php } elseif(get_field('wide_box')): { ?>

                <li class="watcheroo wider" id="<?php echo sanitize_title($post->post_title); ?>">
                    <div class="content" <?php if($bkg_src_lg[0]){ echo 'style="background-image: url(' . $bkg_src_lg[0] . ');"'; } ?> >
                        <div class="overlay">
                            <div class="caption">
                                <h4><?php echo $post->post_title; ?></h4>
                                <div class="info">
                                    <div class="network"><strong>Network:</strong> <?php echo $network; ?></div>
                                    <div class="timeofday"><strong>Time-of-day:</strong> <?php echo $timeofday; ?></div>
                                    <div class="date"><strong>Date:</strong> <?php echo $date; ?></div>
                                    <div class="producer"><strong>Produced by:</strong> <?php echo $producer; ?></div>
                                    <?php //echo apply_filters('the_content', $post->post_content); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="expanded-content">
                        <?php echo apply_filters('the_content', $post->post_content); ?>
                        <?php 
                            $terms = get_the_terms($post->ID,'watcheroo_production_credits');
                            if($terms) {
                                echo '<ul class="credits list-unstyled list-inline">'; 
                                foreach ($terms as $term) {
                                    echo '<li>' . $term->name . '</li>';
                                }
                                echo '</ul>';
                            }
                        ?>
                    </div>
                </li>   

            <?php }

        } else { ?>

            <li class="watcheroo <?php if($addClass){echo 'widewidth'; } ?>" id="<?php echo sanitize_title($post->post_title); ?>">
                <div class="content" <?php if($bkg_src[0]){ echo 'style="background-image: url(' . $bkg_src[0] . ');"'; } ?> >
                    <div class="overlay">
                        <div class="caption">
                            <h4><?php echo $post->post_title; ?></h4>
                            <div class="info">
                                <div class="network"><strong>Network:</strong> <?php echo $network; ?></div>
                                <div class="timeofday"><strong>Time-of-day:</strong> <?php echo $timeofday; ?></div>
                                <div class="date"><strong>Date:</strong> <?php echo $date; ?></div>
                                <div class="producer"><strong>Produced by:</strong> <?php echo $producer; ?></div>
                                <?php //echo apply_filters('the_content', $post->post_content); ?>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="expanded-content">
                    <?php echo apply_filters('the_content', $post->post_content); ?>
                    <?php 
                        $terms = get_the_terms($post->ID,'watcheroo_production_credits');
                        if($terms) {
                            echo '<ul class="credits list-unstyled list-inline">'; 
                            foreach ($terms as $term) {
                                echo '<li>' . $term->name . '</li>';
                            }
                            echo '</ul>';
                        }
                    ?>
                </div>
            </li>

        <?php } 

    } ?>    

    </ul>

     <div class="clearfix"></div>

       <?php } endif; ?>

     </div>

     </div>

     <?php foreach ($postslist as $post) { 

        $network = get_post_meta($post->ID,'network',true);
        $timeofday = get_post_meta($post->ID,'time_of_day',true);
        $date = get_post_meta($post->ID,'date',true);
        $producer = get_post_meta($post->ID,'producer',true);
        $bkg_id =  get_post_thumbnail_id( $post->ID );
        $bkg_src = wp_get_attachment_image_src( $bkg_id, 'medium' ); 

    ?> 

      <div id="<?php echo $post->ID . '-' . sanitize_title($post->post_title); ?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modalwindow">
     <div class="modal-dialog modal-lg">
       <div class="modal-content">
          <div class="modal-body"> 
           <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <?php if($bkg_src[0]){ ?><img class="alignleft" src="<?php echo $bkg_src[0]; ?>" /><?php } ?> 
        <h1 class="modal-title"><?php echo $post->post_title; ?></h1>
        <div class="network"><strong>Network:</strong> <?php echo $network; ?></div>
        <div class="timeofday"><strong>Time-of-day:</strong> <?php echo $timeofday; ?></div>
        <div class="date"><strong>Date:</strong> <?php echo $date; ?></div>
        <div class="producer"><strong>Produced by:</strong> <?php echo $producer; ?></div>
        <?php echo apply_filters('the_content', $post->post_content); ?>
        <div class="clearfix"></div>
      </div>
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
         </div>
     </div>
    </div>
   </div>

   <?php } ?>

Am I placing the endif; in the wrong spot? I tried placing it in different spots, please any help would definitely be appreciated.

  • 写回答

1条回答 默认 最新

  • dongxi3859 2016-10-19 21:07
    关注

    The preferred syntax for PHP conditionals:

    if ($foo) {
       do_something();
    } elseif ($bar) {
       do_something_else();
    } else {
       do_nothing();
    }
    

    It can also be one-lined:

    if ($foo) do_something();
    elseif ($bar) do_something_else();
    else do_nothing();
    

    This is generally not considered good practice, though.

    Finally, one more alternate syntax, which I will quote from the manual:

    if($a > $b):
        echo $a." is greater than ".$b;
    elseif($a == $b): // Note the combination of the words.
        echo $a." equals ".$b;
    else:
        echo $a." is neither greater than or equal to ".$b;
    endif;
    

    Note that "elseif" must be one word in this alternate syntax. It's also hardly used.

    You don't use both syntaxes in one line of code, however ;-)

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

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题