Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn
This will be really helpful on WordPress websites which have thousands of pages. Even if they are categorised it can be hard to find a post.
This code will add a search box to the top of the archive / category pages. The search box will then only display results for that specific category.
There are a lot of plug-ins which will offer similor functionality but you really don’t need one as it’s only a few lines of code.
Firstly to add the category ID to the search form. For this we will edit the default search form which is usually in the theme template in the file searchform.php
Within the form add the following code.
if ( is_category() ) { echo '<input name="cat" type="hidden" value="'.get_query_var('cat')'." />'; }
Those few lines of code will check if the page is a category archive page and if so add a hidden field which had the category ID in.
Then to add this form to the site edit your archive.php or index.php depending on which is being used and add the search form
if ( is_category() ) { get_search_form(); }
This will add the search form to the page if it’s a category page.
For a few lines of code it add’s a lot of functionality to category home pages.
Posted by Adi on April 24, 2014
Leave a Reply