WP_QUERY LOOP ELSE语句未执行

时间:2015-02-28 作者:Boris Kamp

我有两个WP_query 在我的frontpage中循环。php文件:

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

这个<?php endwhile; else: ?> 部件不工作,它只是不显示任何内容,而不是<h1>Kampbeheer heeft momenteel niks te huur!</h1><p>Niks te weergeven hier!</p> 未查询帖子时

我错过了什么?

2 个回复
最合适的回答,由SO网友:s_ferdie 整理而成

尝试更改:

<?php if ( have_posts() ) 

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

SO网友:Manny Fleurmond

功能have_posts 用于主回路,因此if 语句正在检查主循环,它可能总是有POST。您想使用自定义查询的have_posts 对于if 而不是语句。

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

结束

相关推荐

WP update_post_meta link loop

我试图通过循环中的链接更新帖子元。这是代码function make_sticky($post_id) { // Update, add, or delete field ---------------------------------- if ( get_post_meta($post_id, \'sticky\', FALSE ) ) { if ( get_post_meta($post_id, \'sticky\', \'1\' ) ) {&