I have 3 custom searches created using different templates.
The search results page loads different results depending on the referrer:
<?
/* Template Name: Search Results */
$search_refer = $_GET["post_type"];
if ($search_refer == 'post') {
load_template(TEMPLATEPATH . '/search/filme.php');
}
elseif ($search_refer == 'persoane') {
load_template(TEMPLATEPATH . '/search/actori.php');
}
elseif ($search_refer == 'trailers') {
load_template(TEMPLATEPATH . '/search/trailers.php');
}
?>
I’d like to make the url of this page prettier. Right now it looks like this:
/?s=After+Earth&post_type=post
but I’d like it to look like this:
/search/After+Earth/post_type/post
How do I make the url more beautiful?
2 Answers
This snippet should solve your problem. Just put it into your functions.php file.
function my_insert_rewrite_rules( $rules ) {
$newrules = array();
$newrules['search/([^/]+)/post_type/([^/]+)/?$'] = 'index.php?s=$matches[1]&post_type=$matches[2]';
return $newrules + $rules;
}
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
function my_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_flush_rewrite_rules' );
You will have to flush rewrite rules after adding this code, before it will start to work. Just switch theme to another and back or go to Settings->Permalink Settings and push “Save” button.