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 的新手

您使用了错误的语法,请阅读此处,更改:

<? } else { ?>

至:

<? else: ?>

您应该使用colonbrackets语法,但不能同时使用这两种语法。

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

这正是你所需要的。