如何解决WordPress循环的问题?

问题描述:

我已经为自定义帖子类型创建了WordPress循环 - 组合,它确实有效。不过,我需要添加消息,当没有帖子显示,但它以任何方式返回错误,我已经尝试过。如何解决WordPress循环的问题?

这是我工作的WordPress循环:

  $gallery = new WP_Query($args); 

     while($gallery->have_posts()): $gallery->the_post(); 
      if(has_post_thumbnail()): 

      $data_types = ''; 
      $item_cats = get_the_terms($post->ID, 'portfolio_category'); 
      if($item_cats): 
      foreach($item_cats as $item_cat) { 
       $data_types .= $item_cat->slug . ' '; 
      } 
      endif; 

      $full_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'portfolio-full'); ?> 

       <li data-id="<?php echo $post->ID;?>" class="portfolio_item" data-type="<?php echo $data_types;?>"> 
       <a href="<?php the_permalink(); ?>" rel="prettyPhoto" title=""> 
        <span class="pic"><?php the_post_thumbnail('portfolio-medium'); ?><div class="img_overlay"></div></span> 
        <h4><?php the_title(); ?></h4> 
       </a> 
       </li>   

     <?php endif; endwhile; ?> 

我试图关闭这样的循环:

<?php endwhile; ?> 
<?php else : ?> 
<p>My Text Here</p> 
<?php endif; ?> 

取而代之的是当前的:

<?php endif; endwhile; ?> 

我得到错误“语法错误,意外的T_ENDWHILE”

我在这里错过了一些重要的东西吗?

+0

你尝试标准开启和关闭'如果{}',而不是ENDIF ? – Kemal 2013-02-17 21:47:17

+1

该HTML块是非常讨厌的。 divs内跨度?不会修复您的PHP级别错误,但您确实需要开始编写有效的html。 – 2013-02-17 22:10:12

+0

@马克B,感谢您指出这一点!我最近才开始学习如何编写代码,以便欢迎所有的建议和提示! :) – Puikinsh 2013-02-18 12:15:54

是否有帖子中,我想包在if声明,你可以用它来检查循环 - 就像这样:

<?php 
$gallery = new WP_Query($args); // initialize query 
if($gallery->have_posts()): // if there are posts 
    while($gallery->have_posts()) : $gallery->the_post(); // the loop 
    // do stuff with the post here 
    endwhile; // end of the loop 
else: // if there aren't any posts 
    echo "<p>no posts found!</p>"; 
endif; // end of the if statement 
?> 
+0

谢谢!这正是我期待的! – Puikinsh 2013-02-18 12:13:47