Need help hiding text if a query has no posts
-
I received an answer yesterday from MichaelH on how to query a post and search only certain keywords that I specify. Everything works great except if it doesn’t return any posts, it still echos out that it’s trying to display it:
<?php // display post title for any post that has both $findtext1 and $findtext2 in the post content $findtext1 = 'Welcome'; $findtext2 = 'first'; $args=array( 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'showposts' => -1, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"'; while ($my_query->have_posts()) : $my_query->the_post(); if ( (strpos($post->post_content, $findtext1)!== false) && (strpos($post->post_content, $findtext2)!== false)) { ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php } //if (strpos endwhile; } wp_reset_query(); // Restore global post data stomped by the_post(). ?>
This part is what I need help on. It’s still showing this echo statement even when there is no query results.
if( $my_query->have_posts() ) { echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';
Any ideas are welcome and much appreciated!
- The topic ‘Need help hiding text if a query has no posts’ is closed to new replies.