drbd65446 2016-05-18 13:59
浏览 16
已采纳

用else语句麻烦

Im having some trouble with a else statement i'm trying to write. Basicly if the repeater field is there and displays the content do that, if no content is in the repeater field display this.

Here is the original code without the else statement that I started from

                <div class="tabs-panel" id="panel5">
                <?php 
                if ( have_rows( 'ar_content' ) ):
                    $i = 0;
                    $n = count( get_field('ar_content') ); 
                ?>
                    <div class="row">
                        <?php 
                        while ( have_rows( 'ar_content' ) ): 
                            the_row();
                            $i++;
                        ?>
                            <div class="small-12 medium-4 columns">
                                <?php the_sub_field('ar_block'); ?>
                            </div>
                            <? if ($i % 3 == 0 && $i < $n) : ?>
                                </div>
                                <div class="row">
                        <?php 
                            endif;
                        endwhile; 
                        ?>
                    </div>
                <? endif; ?>
            </div><!-- end panel 5 -->

Here is the code I am trying to get working. I would think its would be just replace the last endif with a else and move on the to else content?

                <div class="tabs-panel" id="panel5">
                <?php 
                if ( have_rows( 'ar_content' ) ):
                    $i = 0;
                    $n = count( get_field('ar_content') ); 
                ?>
                    <div class="row">
                        <?php 
                        while ( have_rows( 'ar_content' ) ): 
                            the_row();
                            $i++;
                        ?>
                            <div class="small-12 medium-4 columns">
                                <?php the_sub_field('ar_block'); ?>
                            </div>
                            <? if ($i % 3 == 0 && $i < $n) : ?>
                                </div>
                                <div class="row">
                        <?php 
                            endif;
                        endwhile; 
                        ?>
                    </div>
                <? } else { ?>
                <h2>content to show if nothing is above</h2>
            <?php endif; ?>
            </div><!-- end panel 5 -->

Not working though, any thoughts. And yes if this is totally jacked i'm new to PHP

  • 写回答

2条回答 默认 最新

  • duancifu6769 2016-05-18 14:03
    关注

    You are using wrong syntax, please read here, change:

    <? } else { ?>
    

    to:

    <? else: ?>
    

    You should use either the colon or the brackets syntax, but not both.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度