How to do a search in current category
I am using this code for my search:
<?php
function wp_search_form($form) {
$form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
'.wp_dropdown_categories('exclude=1 Categories&hide_empty=0&echo=0&selected='.intval($_GET['cat']).'').'
<input type="text" class="search_input" value="' . attribute_escape(apply_filters('the_search_query', get_search_query())) . '" name="s" id="s" />
<input type="submit" alt="Search" class="greybutton float_right" val开发者_如何学Goue="Search" />
</div>
</form>';
return $form;
}
I want to remove the catagory dropdown and instead make it search the current catagory
I don't want to steal TheDeadMedic's karma... my rating isn't high enough to comment on existing answers yet...
Here's his code put together:
function wp_search_form($form) {
global $wp_query;
$form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
<input type="hidden" name="cat" value="'. $wp_query->get_queried_object_id() .'" />
<input type="text" class="search_input" value="' . attribute_escape(apply_filters('the_search_query', get_search_query())) . '" name="s" id="s" />
<input type="submit" alt="Search" class="greybutton float_right" value="Search" />
</div>
</form>';
return $form;
}
Remove the wp_dropdown_categories()
call and replace with;
<input type="hidden" name="cat" value="<?php echo $wp_query->get_queried_object_id(); ?>" />
Note if you're using that inside a function, you'll need to globalise $wp_query
.
I make the search in current category with the following code (in searchform.php
or where you want to put a search form:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div>
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="hidden" name="cat" value="<?php echo $cat ;?>" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
精彩评论