# Search template loops break in show element else statement

**URL:** <https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589>\
**Category:** Pinegrow WordPress Plugin\
**Tags:** bug\
**Created:** [January 6, 2023, 4:26pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589 "2023-01-06T16:26:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CarrieTwist](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@CarrieTwist](https://forum.pinegrow.com/u/CarrieTwist)\
**Post date:** [January 6, 2023, 4:26pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/1 "2023-01-06T16:26:18Z")

</div>

In my search.php template, I want to display lists of page and post links when no results are found instead of the “Sorry, no posts matched your criteria.” message.

 ![Screen Shot 2023-01-06 at 10.12.45 AM](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/6/6e8ab558aaf06d9325fceea9f81bc00e739a5fa6.png)

Using the Show Posts Spart Action \>\> In the loop structure \>\> Show if element empty, when I select the div containing the two lists div.no-posts …

 ![main-loop](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/8/83c54d100dc0f10c6b30f7ae3f009f85ae32280b.png)

then the two loops querying the posts and pages on the unordered lists break and the “Sorry, no posts matched your criteria.” message is back.

 ![Loops-Broken](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/a/a0507abd3a7b5fa87f621fbc906511c749bdf74d.png)

When I looked at the code preview of the Main loop on the container, the loops for the page/Post lists are missing in the else statement.

 ![code-preview](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/d/d02a0dbc3a369e9d626bf54d515b66c548c16444.png)

However, I do see the correct PHP in the code preview on each of the loops for the lists, screenshot below.

 ![Screen Shot 2023-01-06 at 11.22.46 AM](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/1/1b4d7916a0c45da17ceb6ab251871f2986709d94.png)

If I paste the code directly into the search.php file via the WordPress theme editor the loops work but then as soon as I save anything in the Pinegrow project, the code is overwritten.

I tried pasting the code (see below) directly into the Pinegrow builder, and this makes everything work on the front end, but then I lose the ability to use the Pinegrow tools to see or edit the template. All I see is the PHP in the editor…

 ![code-in-pinegrow-editor](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/9/9689fc34d65ea76b4474d9ed50d4908405ac7b41.png)

I don’t know if this is a bug or if it has something to do with my setup.

Does anyone have any suggestions for how to solve this?

 \<?php if ( have\_posts() ) : ?\> \<?php while ( have\_posts() ) : the\_post(); ?\> \<?php PG\_Helper::rememberShownPost(); ?\> 
\> 
 \<?php echo PG\_Image::getPostImage( null, 'full', array( 'class' =\> 'img-fluid w-100', 'sizes' =\> '(max-width: 100px) 55vw, (max-width: 576px) 92vw, (max-width: 768px) 70vw, (max-width: 1400px) 29vw, 410px' ), null, null ) ?\>

 \<?php the\_terms( get\_the\_ID(), 'category' ); ?\>
### \<?php the\_title(); ?\>
 \<?php the\_excerpt( ); ?\> 
\<?php echo get\_the\_author\_meta( 'display\_name', false ) ?\>\<?php the\_time( get\_option( 'date\_format' ) ); ?\>

 \<?php endwhile; ?\> \<?php else : ?\> 

## \<?php \_e( 'Nothing to see here...', 'pinegrow\_theme' ); ?\>

### \<?php \_e( 'Posts', 'pinegrow\_theme' ); ?\>
 \<?php $post\_query\_args = array( 'post\_type' =\> 'post', 'nopaging' =\> true, 'order' =\> 'ASC', 'orderby' =\> 'date' ) ?\> \<?php $post\_query = new WP\_Query( $post\_query\_args ); ?\> \<?php if ( $post\_query-\>have\_posts() ) : ?\> 
 \<?php while ( $post\_query-\>have\_posts() ) : $post\_query-\>the\_post(); ?\> \<?php PG\_Helper::rememberShownPost(); ?\> - \>\<?php the\_title(); ?\>
 \<?php endwhile; ?\> \<?php wp\_reset\_postdata(); ?\> 
 \<?php endif; ?\> 

### \<?php \_e( 'Pages', 'pinegrow\_theme' ); ?\>
 \<?php $page\_query\_args = array( 'post\_type' =\> 'page', 'nopaging' =\> true, 'order' =\> 'ASC', 'orderby' =\> 'date' ) ?\> \<?php $page\_query = new WP\_Query( $page\_query\_args ); ?\> \<?php if ( $page\_query-\>have\_posts() ) : ?\> 
 \<?php while ( $page\_query-\>have\_posts() ) : $page\_query-\>the\_post(); ?\> \<?php PG\_Helper::rememberShownPost(); ?\> - \>\<?php the\_title(); ?\>
 \<?php endwhile; ?\> \<?php wp\_reset\_postdata(); ?\> 
 \<?php endif; ?\> 

 \<?php endif; ?\>

```
</div>             

```

---

<div class="post-metadata">

**Author:** ![schpengle](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/schpengle/32/238_2.png) [@schpengle](https://forum.pinegrow.com/u/schpengle)\
**Post date:** [January 6, 2023, 4:47pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/2 "2023-01-06T16:47:17Z")

</div>

Well, I have no idea of such things, but hello and welcome to the Forum 🙂  
just helping out here, if you click the indicated icon when posting code, then it will make it easier for you and others to read and to help you.

I hope someone cleverer than I gets back to you asap.  
Good luck  
🙂

 ![Screenshot 2023-01-06 at 16.42.45](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/a/a4314121f6c8d1200bf46b3ab850a5fa891b5a5b.png)

```auto
[quote="CarrieTwist, post:1, topic:7589"]

<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php PG_Helper::rememberShownPost(); ?>

>

<?php echo PG_Image::getPostImage( null, 'full', array( 'class' => 'img-fluid w-100', 'sizes' => '(max-width: 100px) 55vw, (max-width: 576px) 92vw, (max-width: 768px) 70vw, (max-width: 1400px) 29vw, 410px' ), null, null ) ?>

<?php the_terms( get_the_ID(), 'category' ); ?>

### <?php the_title(); ?>

<?php the_excerpt( ); ?>

<?php echo get_the_author_meta( 'display_name', false ) ?><?php the_time( get_option( 'date_format' ) ); ?>

<?php endwhile; ?> <?php else : ?>

## <?php _e( 'Nothing to see here...', 'pinegrow_theme' ); ?>

### <?php _e( 'Posts', 'pinegrow_theme' ); ?>

<?php $post_query_args = array( 'post_type' => 'post', 'nopaging' => true, 'order' => 'ASC', 'orderby' => 'date' ) ?> <?php $post_query = new WP_Query( $post_query_args ); ?> <?php if ( $post_query->have_posts() ) : ?>

<?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?> <?php PG_Helper::rememberShownPost(); ?>* ><?php the_title(); ?>
<?php endwhile; ?> <?php wp_reset_postdata(); ?>

<?php endif; ?>

### <?php _e( 'Pages', 'pinegrow_theme' ); ?>

<?php $page_query_args = array( 'post_type' => 'page', 'nopaging' => true, 'order' => 'ASC', 'orderby' => 'date' ) ?> <?php $page_query = new WP_Query( $page_query_args ); ?> <?php if ( $page_query->have_posts() ) : ?>

<?php while ( $page_query->have_posts() ) : $page_query->the_post(); ?> <?php PG_Helper::rememberShownPost(); ?>* ><?php the_title(); ?>
<?php endwhile; ?> <?php wp_reset_postdata(); ?>

<?php endif; ?>

<?php endif; ?>

```

```auto
[/quote]

```

---

<div class="post-metadata">

**Author:** ![CarrieTwist](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@CarrieTwist](https://forum.pinegrow.com/u/CarrieTwist)\
**Post date:** [January 6, 2023, 5:36pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/3 "2023-01-06T17:36:09Z")

</div>

Thanks, that’s the screenshot that I showed. but here’s the code for the main loop, if that’s helpful for anyone:

```auto
<div class="container"> 
    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php PG_Helper::rememberShownPost(); ?>
            <div id="post-<?php the_ID(); ?>" <?php post_class( 'mb-4 row' ); ?>>
                <div class="col-md-4"> <a href="<?php echo esc_url( get_permalink() ); ?>" class="d-block mb-3 mb-md-0"><?php echo PG_Image::getPostImage( null, 'full', array(
                                'class' => 'img-fluid w-100',
                                'sizes' => '(max-width: 100px) 55vw, (max-width: 576px) 92vw, (max-width: 768px) 70vw, (max-width: 1400px) 29vw, 410px'
                        ), null, null ) ?></a>
                </div>
                <div class="col-md-8"> 
                    <?php the_terms( get_the_ID(), 'category' ); ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" class="text-dark text-decoration-none"><h3 class="fw-bold h5"><?php the_title(); ?></h3></a>
                    <p><?php echo get_the_excerpt(); ?></p>
                    <div class="border border-start-0 border-end-0 d-flex justify-content-between pb-2 pt-2 small text-dark">
                        <a href="<?php echo get_author_posts_url( false, get_the_author_meta( 'user_nicename', false ) ) ?>" class="text-secondary"><?php echo get_the_author_meta( 'display_name', false ) ?></a>
                        <span><?php the_time( get_option( 'date_format' ) ); ?></span>
                    </div>                     
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <div class="no-posts" wp-loop-empty>
            <div class="sitemap_list">
                <h2>Nothing to see here...</h2>
                <h3>Posts</h3>
                <ul cms-post-repeat="li" cms-post-items-container="this" cms-post-show-empty-text="false" cms-post-type="post" cms-post="query">
                    <li>
                        <a href="#" cms-post-title cms-post-link>Link </a>
                    </li>
                </ul>
            </div>
            <div class="sitemap_list">
                <h3>Pages</h3>
                <ul cms-post="query" cms-post-type="page" cms-post-type-mime="image" cms-post-items-container="this" cms-post-repeat="li" cms-post-show-empty-text="false">
                    <li>
                        <a href="#" cms-post-title cms-post-link>Link </a>
                    </li>
                </ul>
            </div>
        </div>
    <?php endif; ?> 
</div>

<!-- Functions.php section Include Resources -->
if( !class_exists( 'PG_Helper' ) ) { require_once "inc/wp_pg_helpers.php"; }

```

---

<div class="post-metadata">

**Author:** ![schpengle](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/schpengle/32/238_2.png) [@schpengle](https://forum.pinegrow.com/u/schpengle)\
**Post date:** [January 6, 2023, 5:39pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/4 "2023-01-06T17:39:08Z")

</div>

yeah the only thing that jumped out at me was the inclusion of a  
/div tag in yours, with no opening tag I could see.

Could just be where you copied and pasted, so thought I’d do the pretty code thing to see if it helped.  
🙂

_Me no Speak PhPEEE_

Excellent break down of you problem though, very thorough, that should help someone to help you.

---

<div class="post-metadata">

**Author:** ![CarrieTwist](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@CarrieTwist](https://forum.pinegrow.com/u/CarrieTwist)\
**Post date:** [January 7, 2023, 12:58pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/5 "2023-01-07T12:58:50Z")

</div>

[schpengle](https://forum.pinegrow.com/u/schpengle) Could you please tell me where that rogue closing div tag is? I couldn’t find it.

Thanks for taking the time to look at this.

---

<div class="post-metadata">

**Author:** ![matjaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/matjaz/32/5_2.png) [@matjaz](https://forum.pinegrow.com/u/matjaz)\
**Post date:** [January 7, 2023, 4:11pm UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/6 "2023-01-07T16:11:01Z")

</div>

@CarrieTwist “Show element if empty” doesn’t support WP actions on that element.

A workaround is to add PHP code that displays the template part (or calls a custom PHP function).

Here is a quick example of the HTML code with WP actions:

```auto
<div cms-post="loop" cms-post-show-empty-element=".no-posts-found" cms-post-items-container=".items-container" cms-post-repeat="h1">
    <div class="items-container">
        <h1 cms-post-title>Post title</h1>
    </div>
    <div class="no-posts-found">
        <div>
            <?php get_template_part( 'list-posts' ); ?>
        </div>
    </div>
</div>

```

The template part can be created with PG’s “Reusable Template Part” action or with code, if you prefer.

I updated the Show Posts docs with this: [Show Posts | Pinegrow Web Editor](https://pinegrow.com/docs/wordpress/actions/show-posts/#making-the-content-of-no-posts-found-element-dynamic)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![schpengle](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/schpengle/32/238_2.png) [@schpengle](https://forum.pinegrow.com/u/schpengle)\
**Post date:** [January 8, 2023, 12:11am UTC](https://forum.pinegrow.com/t/search-template-loops-break-in-show-element-else-statement/7589/7 "2023-01-08T00:11:19Z")

</div>

Hi @CarrieTwist , sorry for delay was out and about.  
Yes, just right at the end of the code you posted originally. It was just a closing div tag, I couldn’t see the opening one, so I assumed it was just a cut and paste thing and you’d omitted to copy the initial opening tag.

> [@CarrieTwist](#):
>
> \<?php endif; ?\> \<?php endif; ?\>
> 
> ```auto
> </div>             
> 
> ```

that bit there, right at the end.
