开发者

WordPress Search Queries

开发者 https://www.devze.com 2023-01-16 19:26 出处:网络
I have added within my WordPress 3.1 site, the following code at the bottom of my sidebar.php file: <div id=\"search_box\">

I have added within my WordPress 3.1 site, the following code at the bottom of my sidebar.php file:

    <div id="search_box">
      <form id="searchform" action="http://www.service.com/" method="get" role="search">
          <input id="s" type="text" name="s" class="search" value="Search site" size="19" maxlength="80" id="white_box" onfocus="if (this.value=='Search site') this.value = ''"/>
          <input id="searchsubmit" type="image" class="submit" value="submit" src="<?php bloginfo( 'template_url' ); ?>/images/search_btn.jpg" />
      </form>
    </div>

As I have coded this search process myself, when I place a some text within my search text box and press the "Sea开发者_Python百科rch" button, it looks as if, is is calling the page search.php within my theme and displaying the results.

Questions:

1) where/how does it know when I press the "Search" button to go off and call search.php?

2) if possible only, how can I change it to call a different php file instead of the search.php

Thanks.


Use template filter

add_filter( 'template_include', 'template_include', 10 );

and change the template as

function template_include($template)
{

    if(your condition here){
        $template = get_template_directory().'/your-template.php';
    }

    return $template;
}


-1. All requests for a WP site go to a single page that routes them based upon specific criteria. Thus when a search is performed it knows that it is a search and directs to the search.php page.

Specifically it goes to index.php that loads wp-blog-header.php.

-2: Everything you need to know should be right here: http://codex.wordpress.org/Creating_a_Search_Page#Using_the_page.php


  1. Wordpress assumes the request is a search if it sees an 's' GET variable.

  2. You can hook into the template_redirect action (in your theme's functions.php file) and load a different template like so:

    add_action('template_redirect', 'my_template_select');
    function my_template_select() {
      if (is_search()) {
        load_template(TEMPLATEPATH . '/foobar.php');
        exit;
      } 
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号