0

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

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

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> when no posts are queried

What am I missing here?

1
  • rewind_posts() should be wp_reset_postdata(). Remember to add this to both queries just after ``endwhile. Also, your if` statements shoul also be set to your query variable, not just you while() statement and the_post() Feb 28, 2015 at 14:44

2 Answers 2

2

Try to change:

<?php if ( have_posts() ) 

To

<?php if ( $verhuurd_query->have_posts() )
0
1

The function have_posts is for the main loop, so the if statement is checking the main loop, which will probably always have posts. You want to use the custom query's have_posts for the if statement instead.

if ( $verhuurd_query->have_posts() ) : while ( $verhuurd_query->have_posts() ) : $verhuurd_query->the_post(); ?>
1
  • Thanks Manny! that's a concise clear answer! will keep that in mind
    – Boris Kamp
    Mar 1, 2015 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.