Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:59:16+00:00 2026-06-19T00:59:16+00:00

I have created WordPress loop for custom post type – portfolio and it does

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:59:17+00:00Added an answer on June 19, 2026 at 12:59 am

    i would wrap the loop in an if statement, which you can use to check whether there are any posts – like so:

    <?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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a custom post type in WordPress, and in the single post template,
I have created a custom image uploader for my wordpress admin panel and need
I have created a new custom Wordpress theme. Stripping out the Wordpress drag and
I have created a simple Wordpress loop already but now it should loop a
Basically I have a custom post type setup called Parts with over 5,000 posts
In Wordpress I have created a custom field 'LatLonPosition' which contains different positions in
I have created a template file for a custom page inside wordpress plugin directory
I am using advanced custom fields Checkbox feature in wordpress. I have created a
I have successfully created a custom taxonomy in WordPress, and I created a page
I have a need to include an extra style on my post loop so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.