else 语句的问题

trouble with else statment

我在尝试编写 else 语句时遇到了一些问题。基本上,如果转发器字段存在并显示内容,则执行此操作,如果转发器字段中没有内容,则显示此内容。

这是我开始的没有else语句的原始代码

                <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 -->

这是我正在尝试运行的代码。我认为它只是用 else 替换最后一个 endif 并继续到 else 内容?

                <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 -->

虽然没有用,有什么想法。是的,如果这完全被劫持了,我是 PHP

的新手

您使用的语法错误,请阅读here,更改:

<? } else { ?>

至:

<? else: ?>

您应该使用 colon brackets 语法,但不能同时使用。

<div class="tabs-panel" id="panel5">
            <?php
            if ( have_rows( 'ar_content' ) ) {
                $i = 0;
            }
$n = count( get_field('ar_content') );
?>
<div class="row">
<?php
    if(have_rows( 'ar_content' )){
        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>
<?php } else {
        echo "<h2>content to show if nothing is above</h2>";
     }
?>

这就是你需要的。