2012-06-08 8 views
5

WP_Queryオブジェクトが一致を返さないかどうかをテストするにはどうすればよいですか? Iこれはテンプレートであるような何かをできるようにしたいと思います: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    
    } 
?> 

EDIT

良い例、私はクエリが何かを見つけた場合のみ、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(); ?> 

答えて

6

標準のWordpress Loopは、have_posts()を使ってこれを行います。 !そう

($ my_query-> 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    
    } 
?> 
+0

右が、私は、クエリの場合に応じて、ページ上の非クエリ情報を表示するために探しています任意の内容を持っています。この情報は、実際のクエリの横にあるページの別の部分に表示されます。ですから、私はページの上部に単一のWP_Queryオブジェクトを作成し、ページの複数の部分に情報を出力したいと考えています。 これを行う最善の方法は、WP_Queryオブジェクトを一番上に作成し、ページ上に複数のwhileループを作成することです。 – Combobreaker

+0

私が実際にやろうとしていることの新しい例を追加しました。 – Combobreaker

5

は、私はあなたが使用することができます理解します:

//if $my_query does not find anything 

Jusあなたはこのように必要な場所にそれを使用すると

$my_query_found_something = 'not'; 

:Tこのようにパラメータを設定し

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

<?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; ?> 
関連する問題