Quantcast
Viewing all articles
Browse latest Browse all 29

Create A Custom Genesis Search Results Page for No Posts Found

I wanted to create a more helpful search results page for the case where no posts were found. The built-in solution for Genesis is a simple paragraph that reads, “Sorry, No posts found.” That paragraph isn’t wrapped in the normal page divs such as .post and .entry-content either. If your theme has style information tied to those divs, your empty search results may feel… well… a little empty.

Genesis creates the output for this scenario using the function genesis_do_noposts() and the hook genesis_loop_else(). The function includes a filter, genesis_noposts_text if you only want to change the text. Here’s an example you could use in that case.

add_filter('genesis_noposts_text','my_noposts_text');

function my_noposts_text() {

  $my_text = "Rats. I haven't written anything about that yet... try another search.";

  return $my_text;

}

But we want to do much more than that. Since I have a custom child theme with styles on the .post and .entry-content divs AND I have some additional divs I’ve inserted AND I want to print the search term on the page for the user AND I want to include some archive-style stuff to be helpful, I’m going to need a completely new function, not just a text filter. Here’s how to do that.

Step 1: Remove The Default No Posts Action

All of the following code goes in functions.php so open that file and let’s first unhook the default action.

remove_action( 'genesis_loop_else', 'genesis_do_noposts' );

Now if you search for something, you should get a blank content area with no text at all.

Step 2: Write Your Custom Function

Now we’ll create our custom function to accommodate the page content we need and want. I’ll post the code I used and then explain it below.

function my_do_noposts() {

  $term = $_GET['s']; // store the search term in a variable

  echo '<div class="post">';
  echo '<h1 class="entry-title">Search Results: ' . $term . '</h1>';
  echo '<div class="entry-content">';

  printf( '<p>%s</p>', apply_filters( 'genesis_noposts_text', __( 'Sorry. I don\'t have anything in the archives that matches your criteria. Try another search term or use the archive links below.', 'genesis' ) ) );

  include('search-helper.php');
	
  echo '</div><!-- close entry-content -->';
  echo '</div><!-- close post -->';
	
}

The first thing I’m doing here is storing the search term in the variable $term. We’ll use that later when we output the page title with the term appended to it.

The second thing I did there was to echo a couple of div tags – those tags I need for my page content. You may not need these at all. It really depends on the theme you’re using. I’m also echoing an entry-title and you can see that I’ve included the $term variable within the entry-title so that the user understands the search results were based on that value she or he typed in the search field.

The line that starts with printf is the line where you specify the general text message you want to give the reader. Type whatever you want, but be sure to escape apostrophes with a backslash (or don’t use words with apostrophes ;)

The fourth item here then, is an include. This is a special block of PHP and HTML I tweaked from the archives page template so that, in the event no posts were found, the user has some other archive-like options on the page from which to choose. If you’d like this as well, create another file in your theme directory named search-helper.php and paste the following into it.

<div class="archive-page">

	<h4><?php _e( 'Categories', 'genesis' ); ?></h4>
	<ul>
		<?php wp_list_categories( 'sort_column=name&title_li=' ); ?>
	</ul>

	<h4><?php _e( 'Articles by Month', 'genesis' ); ?></h4>
  <p>
	<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
    <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
    <?php wp_get_archives( 'type=monthly&format=option&show_post_count=1' ); ?>
  </select>
  </p>

</div><!-- end .archive-page-->

<div class="archive-page">

	<h4><?php _e( 'Tags', 'genesis' ); ?></h4>
	<ul>
		<?php wp_tag_cloud(); ?>
	</ul>

</div><!-- end .archive-page-->

<h4 style="clear:both"><?php _e( 'Article Listing', 'genesis' ); ?></h4>
<ul>
	<?php wp_get_archives( 'type=postbypost&limit=50' ); ?>
</ul>

If you do not want to include anything extra like this, you can remove that line from the code, but without a search-helper.php file, nothing will be included anyway. You could comment-out the line for use later as well.

The last two lines then close the two divs I opened at the beginning. Don’t wanna forget those or bad things will happen. Make sure you have a closing div for every div you opened.

Step 3: Add Your New No Posts Action

The last step is to add an action for your custom function and hook it up to genesis_loop_else(). You might want to place this line just under the line where you unhooked the default function.

add_action( 'genesis_loop_else', 'my_do_noposts' );

Now you should have a working custom search results page for the case where no posts were found. This solution has no impact on the layout of results if and when posts were found. That’s a different tutorial ;)


Viewing all articles
Browse latest Browse all 29

Trending Articles