我为自定义帖子类型创建了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 :(得分:0)
我会将循环包装在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
?>