Change text "Search results" to category name

From OSClass

With regards to major OSClass themes, the search page will display the text, "Search results", at the top of the page. Take a look at it here:


(Note: on this demo, the Breadcrumbs plugin is installed, but it's not required)

Searchresults.jpg


If the user does a search from the homepage, that text seems appropriate. But what if the user simply clicks on a category or subcategory on the homepage? Currently, it will still say 'search results' even though an actual search wasn't done. A better option would be to display the current category or subcategory name instead. That is what this customization is all about!


Now, here is an example of what this customization will show once implemented if a user clicks on a category called Cars.

Searchresults2.jpg


To make this change, first located this code in your theme file search.php:

                                    <h1>
                                        <strong><?php _e('Search results', 'bcute') ; ?></strong>
                                    </h1>


Next, replace that code with this code:

                                    <h1>
                                	<?php osc_goto_first_category();
					$catName = 'Search Results';
				        if(osc_rewrite_enabled()) {
					    $URL = $_SERVER['REQUEST_URI']; 
					    $directory = pathinfo($URL);
					    $cat = $directory['filename'];
				        }
					$sCategory = Params::getParam('sCategory'); $sCategory2 = Params::getParam('sCategory[]');
                                	while ( osc_has_categories() ) {
				    	  if(osc_category_slug()==$cat || osc_category_id()==$sCategory || osc_category_id()==$sCategory2) $catName=osc_category_name();
                                            if(osc_count_subcategories()) {
                                                while(osc_has_subcategories()) {
				    		    if(osc_category_slug()==$cat || osc_category_id()==$sCategory || osc_category_id()==$sCategory2) $catName=osc_category_name();
						} 
					    }
					} ?>
                                      <strong><?php echo $catName; ?></strong>
                                    </h1>


How Does It Work?

The first half of this code will check to see if Permalinks are enabled. If so, it will read & parse the browser's URL to extract the current category name. If permalinks are disabled, the script will instead retrieve the category ID number from the URL.

Next, the code will run through an array of all existing categories and subcategories, comparing either the parsed ID or category name to that in the array. If they match, the category name is stored as $catName, then displayed on the page. If an actual search is performed, this will still display "Search Results" as expected.

Room for improvement: Since in the case of permalinks being enabled, the script gets the current page name and can essentially display that on the page right then, skipping the second-half of the code. That will require just a bit more work.


That's it!

Notes and references

  1. See this OSClass.org forum topic


--Jesse 14:15, 21 December 2011 (CET)