Title: If the loop doesn&#039;t exist
Last modified: August 31, 2016

---

# If the loop doesn't exist

 *  Resolved [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/)
 * I’m using loop exists, and I was wondering if it was possible to execute code
   if the loop doesn’t exist, along the lines of else/if. Thanks.
 * [https://wordpress.org/plugins/custom-content-shortcode/](https://wordpress.org/plugins/custom-content-shortcode/)

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Author [Eliot Akira](https://wordpress.org/support/users/miyarakira/)
 * (@miyarakira)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275671)
 * In the latest plugin update, I added a way to display something when `[loop exists]`
   returns empty.
 *     ```
       [loop exists ...]
         [the-loop]
          ..
         [/the-loop]
         [if empty]
           Nothing found. <----
         [/if]
       [/loop]
       ```
   
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275672)
 * Amazing, thank you.
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275675)
 * I just realized something. In my code I have content before and after the [the-
   loop]. For example:
 *     ```
       [loop exists]
       <table>
       [the-loop]
       <tr><td>[field title]</td></tr>
       [/the-loop]
       </table>
       [/loop]
       ```
   
 * In your example, where [if empty] is placed right after the [the-loop], what 
   would the right syntax be when there is content after [the-loop]? Would it make
   more sense to put the [if empty] code after the [/loop]? of would you just put
   it like:
 *     ```
       [loop exists]
       <table>
       [the-loop]
       <tr><td>[field title]</td></tr>
       [/the-loop]
       </table>
       [if empty]
       Nothing found.
       [/if]
       [/loop]
       ```
   
 * Thanks.
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275676)
 * Hmmm, I tried something like the above, but the problem is that I’m passing a
   list to the loop exists, and it cycle through five times, and each time it’s 
   empty it shows me the code for when it’s empty. Is there a way to display a single
   result for all five passes?
 *  Plugin Author [Eliot Akira](https://wordpress.org/support/users/miyarakira/)
 * (@miyarakira)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275678)
 * The last code you posted looks good – if nothing exists, it will display only
   what’s within [if empty] – so it will not display a table.
 * As for passing a list and showing a single result.. You might be able to achieve
   it with the Math module (enabled under Settings), to count the number of empty
   loops.
 *     ```
       [set total]0[/set]
   
       [pass list..]
         [loop]
           ..some loops..
           [calc]total = total + 1[/calc]
         [/loop]
       [/pass]
   
       [if var=total value=0 compare=more]
         ..One or more loops were empty..
       [/if]
       ```
   
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275679)
 * Interesting. That would work in this case because I know how many times the loop
   goes around. But what if I only want to to show the content when all the loops
   were empty? Something like?:
 *     ```
       [if var=total value=0 compare=equals]
         ..If all loops were empty..
       [/if]
       ```
   
 * I couldn’t find compare=equals, so I’m guessing that’s not exactly right. What’s
   the right way to do that?
 * Thanks.
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275680)
 * or is it simply:
 * `[if var=total value=0]
    ..One or more loops were empty.. [/if]’
 *  Plugin Author [Eliot Akira](https://wordpress.org/support/users/miyarakira/)
 * (@miyarakira)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275681)
 * Yes, that last one should work.
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275682)
 * Hmm. Doesn’t seem to work. I have the [set] before [pass] and [loop exists] then
   the [calc] +1 in [the-loop], and the [if var…] after the [/pass]. I then load
   a page that has not children, which should mean the value of total is still zero,
   but it doesn’t execute the if code.
 *  Plugin Author [Eliot Akira](https://wordpress.org/support/users/miyarakira/)
 * (@miyarakira)
 * [10 years ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275683)
 * Oops, you’re right, the check for empty or zero value was not working right. 
   Please try it with the newest plugin update.
 *  Thread Starter [philipt18](https://wordpress.org/support/users/philipt18/)
 * (@philipt18)
 * [10 years ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275684)
 * Now working, thanks.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘If the loop doesn't exist’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-content-shortcode_766976.svg)
 * [Custom Content Shortcode](https://wordpress.org/plugins/custom-content-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-content-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-content-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-content-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-content-shortcode/reviews/)

## Tags

 * [else](https://wordpress.org/support/topic-tag/else/)
 * [exists](https://wordpress.org/support/topic-tag/exists/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * 11 replies
 * 2 participants
 * Last reply from: [philipt18](https://wordpress.org/support/users/philipt18/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/if-the-loop-doesnt-exist/#post-7275684)
 * Status: resolved