Title: Loop exists / if empty
Last modified: July 3, 2021

---

# Loop exists / if empty

 *  [studioavanti](https://wordpress.org/support/users/studioavanti/)
 * (@studioavanti)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/loop-exists-if-empty/)
 * Hi,
 * I’m looping though hierarchical pages to build a menu and need to distinguish
   pages with sub pages from the ones that don’t have sub pages.
 * The final output should look like that:
 *     ```
       PAGE 1 (has sub pages)
       - SUB PAGE
       PAGE 2
       PAGE 3 (has sub pages)
       - SUB PAGE
       ```
   
 * Here’s my code:
 *     ```
           <ul>
           [loop type=page parent=top-parent-page orderby=menu]
               [-loop exists parent=this orderby=menu]
               <li class="has-submenu">[field title-link]
                   <ul class="submenu">
                       [the-loop]
                       <li>[field title-link]</li>
                       [/the-loop]
                   </ul>
               </li>
               [if empty]<li>[field title-link]</li>[/if]        
               [/-loop]
           [/loop]
           </ul>
       ```
   
 * The nested loop fetches sub pages if the loop’s not empty, and i use `[if empty]`
   for the pages without children.
 * The problem is `[if empty]` doesn’t output anything and i get this result:
 *     ```
       PAGE 1
       - SUB PAGE
       PAGE 3
       - SUB PAGE
       ```
   
 * Page 2 (no children) is removed.
 * [Update] Actually i realize `[the-loop]` doesn’t output anything here either.
 * Any idea please?
    Thanks for your help. 😉
    -  This topic was modified 4 years, 11 months ago by [studioavanti](https://wordpress.org/support/users/studioavanti/).

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

 *  Thread Starter [studioavanti](https://wordpress.org/support/users/studioavanti/)
 * (@studioavanti)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/loop-exists-if-empty/#post-14622772)
 * Short version: how to add a specific `has-submenu` class to `<li>` with nested`
   <ul>` only (sub pages)?
 * Maybe i better use JS afterward?
    -  This reply was modified 4 years, 11 months ago by [studioavanti](https://wordpress.org/support/users/studioavanti/).
 *  Thread Starter [studioavanti](https://wordpress.org/support/users/studioavanti/)
 * (@studioavanti)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/loop-exists-if-empty/#post-14622861)
 * After all, it’s much simpler with jQuery:
 * `$('.my-menu > ul > li > ul').parent().addClass('has-submenu');`

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

The topic ‘Loop exists / if empty’ 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/)

 * 2 replies
 * 1 participant
 * Last reply from: [studioavanti](https://wordpress.org/support/users/studioavanti/)
 * Last activity: [4 years, 11 months ago](https://wordpress.org/support/topic/loop-exists-if-empty/#post-14622861)
 * Status: not resolved