Dan Crowe
Dan Crowe

Reputation: 115

trouble with else statment

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

Upvotes: 0

Views: 66

Answers (2)

M K
M K

Reputation: 9416

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.

Upvotes: 5

MoHo
MoHo

Reputation: 2583

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

This is what you need.

Upvotes: 0

Related Questions