【问题标题】:Customize Search Form Text Input Field Name自定义搜索表单文本输入字段名称
【发布时间】:2012-01-17 19:30:12
【问题描述】:

我想将 wordpress 搜索小部件的默认名称“s”更改为其他名称。所以,基本上我需要更改小部件模板和读取 GET 输入的处理程序。你能帮我快速找到那两个地方吗?谢谢。

【问题讨论】:

    标签: wordpress search customization


    【解决方案1】:

    这是我使用的解决方案,其中 'q' 是搜索字段的名称:

    function my_query_vars( $public_query_vars ) {
        if ( isset( $_GET['q'] ) && ! empty( $_GET['q'] ) ) {
            $_GET['s'] = $_GET['q'];
        }
    
        return $public_query_vars;
    }
    
    add_filter( 'query_vars', 'my_query_vars' );
    

    【讨论】:

      【解决方案2】:

      在google上搜索“get search form wordpress”

      取自这里:http://codex.wordpress.org/Function_Reference/get_search_form

      把这个函数放到你主题的functions.php中

      function my_search_form( $form ) {
      
              $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
              <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
              <input type="text" value="' . get_search_query() . '" name="s" id="s" />
              <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
              </div>
              </form>';
      
              return $form;
          }
      
          add_filter( 'get_search_form', 'my_search_form' );
      

      【讨论】:

      • 好吧,它并没有完全回答我的问题。我也是通过搜索得到的。但是,当我提交搜索表单时,wordpress 将对其进行处理并在查询字符串中查找“s”,我需要它来处理“my_custom_name”查询字符串值的值。因此,必须有某种方式告诉 wordpress 我使用的是“my_custom_name”而不是“s”。或者至少传递搜索字符串。
      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多