I have created WordPress loop for custom post type – portfolio and it does work. However I need to add message when no posts are displayed but it returns error in any way I have tried.
Here is my working WordPress loop:
$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; ?>
And I have tried to close the loop like this:
<?php endwhile; ?>
<?php else : ?>
<p>My Text Here</p>
<?php endif; ?>
Instead of the current:
<?php endif; endwhile; ?>
I get error “syntax error, unexpected T_ENDWHILE”
Am I missing something important here?
i would wrap the loop in an
ifstatement, which you can use to check whether there are any posts – like so: