测试WP_Query对象是否为空

时间:2012-06-08 16:13:13

标签: wordpress

如何测试WP_Query对象是否不返回任何匹配项?我希望能够做这样的事情是模板:

<?php
    $my_query = new WP_Query(array('post_type' => 'biographies'))
    if( ***HOW DO I TEST $my_query HERE*** ) {
        //if $my_query finds anything loop through it here
    } else {
        //if $my_query does not find anything            
    }
?>

修改

更好的例子,我想只在查询找到任何内容时才显示h2:

<?php
    $outside_leasing_query = new WP_Query(array( 'post_type' => 'resin_biographies', 'tax_query' => array(
        'relation' => 'AND',
        array( 'taxonomy' => 'resin_buildings', 'field' => 'slug', 'terms' => $page_slug ),
        array( 'taxonomy' => 'resin_leasing_companies', 'field' => 'slug', 'terms' => 'rubenstein-partners', 'operator' => 'NOT IN' )
    ) )); // resin_buildings taxonomy term slug must match page slug
?>

<h2>Outside Leasing Contacts</h2>

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

    <article <?php post_class('group'); ?>>
        <?php
        if( get_post_meta( $post->ID, '_biography_headshot', true ) != '' ) {
            echo '<img class="contact-thumb" src="' . get_post_meta( $post->ID, '_biography_headshot', true ) . '" alt="'. get_the_title() .'" />';
        } else {
            echo '<img class="contact-thumb-placeholder" src="' . get_bloginfo('template_url') . '/images/default_headshot.jpg" alt="'. get_the_title() . '" />';
        }
        ?>
        <div class="contact-info">
            <hgroup>
                <?php the_title( '<h3>', '</h3>' ); ?>
                <h4 class="contact-title"><?php echo get_post_meta( $post->ID, '_biography_title', true ); ?></h4>
            </hgroup>
            <div class="contact-address"><?php echo wpautop( get_post_meta( $post->ID, '_biography_address', true ) ); ?></div>
            <div class="contact-tel"><span>T</span> <?php echo get_post_meta( $post->ID, '_biography_tel', true ); ?></div>
            <?php if( get_post_meta( $post->ID, '_biography_fax', true ) != '' ) { ?>
                <div class="contact-fax"><span>F</span> <?php echo get_post_meta( $post->ID, '_biography_fax', true ); ?></div>
            <?php } ?>
            <div class="contact-email"><a href="mailto:<?php echo get_post_meta( $post->ID, '_biography_email', true ); ?>"><?php echo get_post_meta( $post->ID, '_biography_email', true ); ?></a></div>
        </div>
    </article>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

3 个答案:

答案 0 :(得分:6)

标准的Wordpress循环使用have_posts()为您完成此任务。

<?php
$my_query = new WP_Query(array('post_type' => 'biographies'));
if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
?>
<!-- YOUR POST(S) -->
<?php endwhile; else : ?>
<p>No posts found</p>
<?php endif; ?>

答案 1 :(得分:5)

我认为你可以使用:

($ my_query-&GT; have_posts())

这样:

<?php
    $my_query = new WP_Query(array('post_type' => 'biographies'))
    if( !($my_query->have_posts())) {
        //if $my_query finds anything loop through it here
    } else {
        //if $my_query does not find anything            
    }
?>

答案 2 :(得分:0)

在您的第一个示例中,您有评论:

//if $my_query does not find anything

只需设置如下参数:

$my_query_found_something = 'not';

然后在任何需要的地方使用它:

if ($my_query_found_something == 'not') {
// keep looking
}