Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

WP_Query loop else statement not executing

Hi guys!

I have two WP_query loops in my frontpage.php file:

<!-- Huidig Aanbod -->
<div id="aanbod">
    <div class="container-fluid section-name-cont">
        <div class="container">
            <div class="row">
                <div class="col-xs-12 section-name"><h1>Aanbod</h1></div>
            </div>
        </div>
    </div>
    <?php
        $args = array( 
            'post_type'         => 'vastgoedobject',
            'meta_key'          => 'status', 
            'meta_value'        => array('Te Huur', 'Binnenkort te huur'),
            'posts_per_page'    =>-1,
        );
        $aanbod_query = new WP_Query( $args );

        if ( have_posts() ) : while ( $aanbod_query->have_posts() ) : $aanbod_query->the_post(); 
    ?>

      <div class="content-section-<?php if( $the_query->current_post%2 == 1 ){ echo 'b';}else{ echo 'a';} ?>">
      <div class="container">
        <div class="row">
            <div class="col-lg-5 <?php if( $the_query->current_post%2 == 1 ){ echo 'col-lg-offset-1 col-sm-push-6 ';} ?>col-sm-6 lead-parent">
                <div class="clearfix"></div>
                <a href="<?php the_permalink(); ?>"><h2 class="section-heading"><?php the_field('straat'); ?> <?php the_field('huisnummer'); ?>, <?php the_field('stad'); ?></h2></a>
                <span class="label label-default"><?php the_field('status'); ?></span>
                <span class="label label-default"><?php echo $euro ;?><?php the_field('totale_huurprijs'); ?>,-</span>
                <span class="label label-default"><?php the_field('verdieping'); ?></span>
                <span class="label label-default"><?php the_field('aantal_kamers'); ?>-kamer <?php the_field('beschrijving_vastgoed'); ?></span>
                <span class="label label-default"><?php the_field('totaaloppervlak'); ?> <?php echo $m2 ;?></span>
                <?php the_field('beschrijving'); ?>
                <a href="<?php the_permalink(); ?>"><button type="button" class="btn btn-default">Bekijk <?php the_field('beschrijving_vastgoed'); ?></button></a>
            </div>
            <div class="col-lg-5 <?php if( $the_query->current_post%2 == 1 ){ echo 'col-sm-pull-6';}else{ echo 'col-lg-offset-2';} ?> col-sm-6">
                <a href="<?php the_permalink(); ?>" class="foto-wrapper">
                    <?php if ( !$detect->isMobile() ) {
                    echo get_image_object_acf('img-responsive img-rounded', 'false', 'foto', '', 'fp-aanbod', 'glyphicon-share-alt');
                    } else {
                    echo get_image_object_acf('img-responsive img-rounded', 'false', 'foto', '', 'fp-aanbod', '');
                    } ?>
                </a>
            </div>
        </div>
      </div>
    </div>

    <?php endwhile; else: ?>

    <h1>Kampbeheer heeft momenteel niks te huur!</h1>

    <?php 
        endif; 
        rewind_posts(); 
    ?>
</div>

<!-- Onlangs Verhuurd -->
<div id="onlangs-verhuurd">
    <div class="container-fluid section-name-cont">
        <div class="container">
            <div class="row">
                <div class="col-xs-12 section-name"><h1>Onlangs Verhuurd</h1></div>
            </div>
        </div>
    </div>
    <?php
      $args = array( 
        'post_type'         => 'vastgoedobject',
        'meta_key'          => 'status', 
        'meta_value'        => array('Verhuurd'),
        'posts_per_page'    => 4,
        );
      $verhuurd_query = new WP_Query( $args );
    ?>

    <?php if ( have_posts() ) : while ( $verhuurd_query->have_posts() ) : $verhuurd_query->the_post(); ?>

      <div class="content-section-<?php if( $the_query->current_post%2 == 0 ){ echo 'b';}else{ echo 'a';} ?>">
      <div class="container">
        <div class="row">
            <div class="col-lg-5 <?php if( $the_query->current_post%2 == 0 ){ echo 'col-lg-offset-1 col-sm-push-6 ';} ?>col-sm-6 lead-parent">
                <div class="clearfix"></div>
                <h2 class="section-heading"><?php the_field('straat'); ?>, <?php the_field('stad'); ?></h2>
                <?php if(get_field('totaaloppervlak')) { ?><span class="label label-default"><?php the_field('totaaloppervlak'); ?> <?php echo $m2 ;?></span><?php } ?>
                <?php the_field('beschrijving'); ?>
            </div>
            <div class="col-lg-5 <?php if( $the_query->current_post%2 == 0 ){ echo 'col-sm-pull-6';}else{ echo 'col-lg-offset-2';} ?> col-sm-6 foto-wrapper">
                <?php echo get_image_object_acf('img-responsive img-rounded', 'false', 'foto', '', 'fp-aanbod', '') ?>
            </div>
        </div>
      </div>
    </div>

    <?php endwhile; else: ?>

    <p>Niks te weergeven hier!</p>

    <?php endif; ?>
</div>

The

<?php endwhile; else: ?>

part is not working, it is just displaying nothing instead of

<h1>Kampbeheer heeft momenteel niks te huur!</h1>

or

<p>Niks te weergeven hier!</p>

What am I missing here?