• Resolved Colossus Designs

    (@colossus-designs)


    Hey all! I’ve been customizing my search.php to show the results in the same grid style that I use across the site for content, and I figured it out miraculously. However, I created another issue. If you search for something that doesn’t exist, you don’t get a “no results” message and the page slightly breaks. I’m not fluent in php just know enough to break things. Can anyone help me out? See the code below.

    website
    http://galeriewestdev.com.s195680.gridserver.com/

    <?php get_header(); ?>
    
    <div id="main-content">
    	<div class="container">
    		<div id="content-area" class="clearfix">
    			<div id="left-area">
    			<div class="search-info">
    				<h1 class="search-title"># <?php echo $s ?></h1>
    			</div>
    		<?php 
    
    $args = array(
        's'         => $_REQUEST['s'],
        'showposts' => -1,
        'any' => $searchable_types // define searchable Post Types
    );
     $tp_allsearch = new WP_Query($args);
    ?>
    
    				<?php
    			// Start the loop.
    			$posts = array();
    // If there are Search posts
    if($tp_allsearch->have_posts()) :
    
        // Go through result
        while($tp_allsearch->have_posts()) : $tp_allsearch->the_post();
    
        // Save Post ID in array
        $posts[] = $post->ID;
    
    endwhile;
    else:
        return false;
    endif;
    		?>
    
    				<?php
    // Build shortcode with the $post array build before
    $the_content = do_shortcode('[ess_grid alias="gd2" posts="'.implode(',', $posts).'"]');
    
    // Echo Out the result in your page
    echo $the_content;
    ?>
    
    			</div> <!-- #left-area -->
    
    			<?php get_sidebar(); ?>
    		</div> <!-- #content-area -->
    	</div> <!-- .container -->
    </div> <!-- #main-content -->
    
    <?php get_footer(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey there,

    right now you’re basically telling WordPress just to return and do nothing, if no results are found. This happens after the “else” statement. Try something like this:

    <?php get_header(); ?>
    
    <div id="main-content">
    	<div class="container">
    		<div id="content-area" class="clearfix">
    			<div id="left-area">
    			   <div class="search-info">
    			  	<h1 class="search-title"># <?php echo $s ?></h1>
    			   </div>
    
    		    <?php 
    
              $args = array(
                  's'         => $_REQUEST['s'],
                  'showposts' => -1,
                  'any' => $searchable_types // define searchable Post Types
              );
               $tp_allsearch = new WP_Query($args);
    
              // Start the loop.
              $posts = array();
    
              // If there are Search posts
              if($tp_allsearch->have_posts()) :
    
                // Go through result
                while($tp_allsearch->have_posts()) : $tp_allsearch->the_post();
    
                  // Save Post ID in array
                  $posts[] = $post->ID;
    
                endwhile;
    
                // Echo out the results here instead of a separate php block
                // Build shortcode with the $post array build before
                $the_content = do_shortcode('[ess_grid alias="gd2" posts="'.implode(',', $posts).'"]');
    
                // Echo Out the result in your page
                echo $the_content;
    
              else:
    
                 // If we dont find any results show some no results stuff here
    
              endif;
    
            ?>
    
    			</div> <!-- #left-area -->
    
    			<?php get_sidebar(); ?>
    		</div> <!-- #content-area -->
    	</div> <!-- .container -->
    </div> <!-- #main-content -->
    
    <?php get_footer(); ?>

    Let me know, if this helps.

    Thread Starter Colossus Designs

    (@colossus-designs)

    Beautiful. Massive step in the right direction. How would I say, “No results, try searching for something else!” Not sure how to insert that text without breaking the php.

    Thanks so much!

    Thread Starter Colossus Designs

    (@colossus-designs)

    Got this figured out. Thanks Keymasters and thanks Google!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Customizing "No Results" in search.php’ is closed to new replies.