【发布时间】:2014-02-13 19:38:22
【问题描述】:
我在 WordPress 循环中使用 WP_Query,由于某种原因,我无法让“else”部分工作。基本上我正在寻找 59 类别的帖子。如果没有,我想显示一些文本。当类别中的任何帖子存在时,循环工作正常,但如果没有,则不会显示任何内容。似乎无法弄清楚为什么这不起作用。这是我的代码。非常感谢任何帮助!
<?php
//The Query
$custom_posts = new WP_Query();
$custom_posts->query('cat=59');
//The Loop
if ( have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
<article>
<div class="thenews">
<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Go to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<h2>Posted on: <?php the_time('F jS, Y') ?></h2>
<?php the_excerpt(); ?>
</div><!-- thenews div ENDS here -->
<div class="clearfloats"><!-- clears the elements above --></div>
</article>
<?php endwhile; else: ?>
<article>
<div class="thenews">
<p>Nothing here to see.</p>
</div><!-- thenews div ENDS here -->
<div class="clearfloats"><!-- clears the elements above --></div>
</article>
<? endif;
//Reset Query
wp_reset_query();
?>
【问题讨论】:
标签: php wordpress if-statement