我有以下WP\\u查询,效果很好:
<h4>Frequently Asked Questions</h4>
<ul class="faq">
<?php
$args = array(
\'post_type\' => \'questions\',
\'posts_per_page\' => \'3\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'types\',
\'field\' => \'slug\',
\'terms\' => \'customer-service\'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
如您所见,查询的顶部有一个标题,我想找到一种仅在查询中有值时显示该标题的方法。如果没有,如果没有问题,标题仍然会显示出来,看起来很奇怪。
我如何检查查询中是否有值?
谢谢
最合适的回答,由SO网友:Bainternet 整理而成
稍微更改一下,然后使用have\\u posts方法检查是否有任何结果:
<?php
$args = array(
\'post_type\' => \'questions\',
\'posts_per_page\' => \'3\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'types\',
\'field\' => \'slug\',
\'terms\' => \'customer-service\'
)
)
);
$loop = new WP_Query( $args );
if ($loop->have_posts()){
?>
<h4>Frequently Asked Questions</h4>
<ul class="faq">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php }