framework icon indicating copy to clipboard operation
framework copied to clipboard

Wordpress Loop when no results

Open Crinsane opened this issue 10 years ago • 5 comments

The frameworks gives us the nice @loop and @endloop methods for our views, which of course is great. But it seems like it has no way of specifing output when there are no posts in the loop. For instance, when you're on a search page, if the search query provides no results, you would want to show a message like "No results for these search terms".

Something like would be great:

@loop
    // Show the post
@empty
    // Show no results message
@endloop

Of course the same should be available for '@query'.

What do you guys think?

Crinsane avatar Dec 18 '14 18:12 Crinsane

I already thought about this but it doesn't work. If we insert the @empty statement, the @endloop has no clue about it should reset or not the query.

For those case scenarios, simply use the @if statement combined with the have_posts() function like so:

@loop
     // Show the posts
@endloop

// No results ?
@if(!have_posts())
    // Show the no result message.
@endif

The same issue appears with the @query statement. It would be great to have shortcuts for everything but I haven't found a way to do it until now :(

jlambe avatar Dec 18 '14 19:12 jlambe

Yes, I've figured as much. One of the possibilty would be some kind of global var that specifies if the while loop is already ended, so you would get something like this:

@loop // set global $whileStopped = false
@empty // set global $whileStopped = true
@endloop // (if global $whileStopped == true => endif) else (if global $whileStopped == false endwhile; endif;)

I agree, not the most beautiful solution, but it might be possible. But maybe it could be a problem if you have nested @loop's?

Of what about a different syntax for this? @ifloop or something?

But if it's not possible, just a normal have_posts() wouldn't be that difficult ofcourse ;) I assume the Loop:: methods will work like normal?

Crinsane avatar Dec 18 '14 19:12 Crinsane

Hmm... In theory, if we have nested loops, this might be a problem. The var name should be "dynamically" created so it's not overwritten.

I think for now we could stick with the simple @if statement until we found a better solution.

Yes the Loop class works if you're inside a WordPress loop however you create it:

  • by using WordPress core functions/classes (have_posts(), WP_Query)
  • by using the scout statements (@loop, @query)

jlambe avatar Dec 18 '14 19:12 jlambe

What if you wanted to do this with @query?

joelataylor avatar Jan 07 '15 09:01 joelataylor

Well currently it is the same issue for @query statement. If you need to use an else statement for your custom loop, you need to stick with @if, @else commands.

Only pass the WP_Query object to your view and use the if-else statements like so:

@if($query->have_posts())
    // Posts code
@else
    // No posts code
@endif

The main issue is the @endloop and @endquery statements. If you do not need to check when there are no posts, the end statements calls WordPress reset loop functions. But if you need to check for no posts, then the reset loop functions should be called at the else statement in place of the closing one.

@Crinsane proposed one way to look at it.

jlambe avatar Jan 07 '15 10:01 jlambe