我使用WP\\u Query来查询和显示事件。不过,在某些情况下,没有要显示的事件,我希望在发生这种情况时显示一条友好的消息,而不是什么都不显示,但我不知道如何做到这一点。如果是mysql查询,我知道我可以使用If(mysql\\u num\\u rows($result)==0),但我认为这不适用于WP\\u查询。还有什么别的办法可以奏效吗?
非常感谢你能给我的任何帮助<莎拉
我使用WP\\u Query来查询和显示事件。不过,在某些情况下,没有要显示的事件,我希望在发生这种情况时显示一条友好的消息,而不是什么都不显示,但我不知道如何做到这一点。如果是mysql查询,我知道我可以使用If(mysql\\u num\\u rows($result)==0),但我认为这不适用于WP\\u查询。还有什么别的办法可以奏效吗?
非常感谢你能给我的任何帮助<莎拉
这么说吧$query
是您的WP\\U查询对象。一、 e。
$query = new WP_Query($some_query_args );
然后您可以通过以下方式设置“循环” $query->get_posts();
然后检查是否确实有任何返回的结果: if ( $query->have_posts() ) :
//Use a While loop to show the results
else:
//No results, let\'s show a message instead.
//This is how WordPress\' twentyeleven theme does it, but you can format it how you like:
?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( \'Nothing Found\', \'twentyeleven\' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( \'Apologies, but no events were found.\', \'twentyeleven\' ); ?></p>
</div>
</article>
<?php
endif;
请记住,逻辑必须位于php标记内部,而HTML标记必须位于外部。免责声明:语法可能不完全正确,我还没有测试过。但请查看索引。看看他们做了什么
我使用的是最新的Wordpress版本,默认主题。我有一个类似于此的函数,它包含在页面中并从页面中调用(使用exec-PHP)。<?php function myFunc($value) { while () // some condition { echo $value; } ?> <?php myFunc(\'val\'); ?> 的输出echo 函数出现在页面底部,