【问题标题】:Else Portion of WordPress Loop Not WorkingWordPress 循环的其他部分不起作用
【发布时间】: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


    【解决方案1】:

    您没有使用自定义循环的 have_posts 方法,这就是 else 没有触发的原因。

    变化:

    //The Loop
    if ( have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
    

    收件人:

    //The Loop
    if ( $custom_posts->have_posts() ) : while ($custom_posts->have_posts()) : $custom_posts->the_post();
    

    【讨论】:

    • 谢谢!抱歉,我花了一段时间才回复您,我希望收到任何回复的自动通知,但从未发生过。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多