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.
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 …
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.
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.
However, I do see the correct PHP in the code preview on each of the loops for the lists, screenshot below.
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…
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 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>
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
[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; ?>
[/quote]
Thanks, that’s the screenshot that I showed. but here’s the code for the main loop, if that’s helpful for anyone:
<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"; }
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.
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.
matjaz
January 7, 2023, 4:11pm
6
@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:
<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
Hope this helps!
1 Like
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; ?>
</div>
that bit there, right at the end.