I need a custom search page to lead to search.php but it goes to index.php

I built a custom search page with a form like:<form role="search" method="get" id="searchfor

I built a custom search page with a form like:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">

trouble is I want the results to appear in search.php

whereas I m led to index.php

How can I force to always go to search.php ?

I built a custom search page with a form like:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">

trouble is I want the results to appear in search.php

whereas I m led to index.php

How can I force to always go to search.php ?

Share Improve this question asked Sep 22, 2012 at 3:57 p.a.p.a. 5813 gold badges13 silver badges29 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

If I get it correctly Wordpress loads the search template dynamically by the parameters delivered to it. Specifically the s parameter. Meaning if you try to send the user to the http://www.example/search.php you'll get 404 error page.
For now the only workaround I can think is to create a function which is hooked to the init action hook that listens for a parameter in order to load the right file. The function must reside in the functions.php of your theme (assuming you are developing a theme). The function goes like this:

function load_custom_search_template(){
    if(isset($_REQUEST['custom_search'])){
        require('my_search.php');
        die();
    }
}
add_action('init','load_custom_search_template');

Template tag for the rescue

Simply use get_search_form( $echo ) for the search form.

This template tag first searches for a searchform.php file in your main theme folder. If there is no such template, then you'll get a default search form. And depending on your input argument, you can echo or just return it for later use. It also has a filter on its end, that contains the complete search form HTML as string: get_search_form with one argument: $form_html.

How and what to use for search form modifications

So if you want to alter the output, just append, prefix, replace whatever...

  • use the get_search_form-filter to pre-/append something or completely replace the form
  • use the custom template to build a unique search form

Or get really sophisticated with theme structure and use the filter to load your searchform from somewhere in your nested theme structure:

// In your functions.php
function wpse65934_searchform_location( $form )
{
    return get_template_part( 'subfolder/searchform', 'default' );
}
add_filter( 'get_search_form', 'wpse65934_searchform_location' );

Try adding a hidden input of keyword search, as it'll redirect to the search.php template

<input type="hidden" name="s" id="search" value="<?php the_search_query(); ?>" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170991a4614922.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信