【问题标题】:Form Action - Custom URL表单操作 - 自定义 URL
【发布时间】:2014-12-17 06:19:45
【问题描述】:

我的 wordpress 主题中有一个搜索表单。我想在搜索结果中添加一个自定义 url。

我的源代码:

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="text" name="s" id="s" onblur="if (value =='') {value = 'Search'}" onfocus="if (value == 'Search') {value =''}" value="Search" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( '', 'themevb' ); ?>" />
</form>

我的实际搜索网址:

http://www.example.com/?s=keyword&submit=

我需要这个网址:

http://www.example.com/web/keyword

【问题讨论】:

    标签: html wordpress forms themes wordpress-theming


    【解决方案1】:

    最简单的解决方案,将此脚本加入队列,或者只是放在搜索表单下方,它将导航到自定义搜索页面。

    <script>
    jQuery(function($){
        $( '#searchform' ).on( 'submit', function() {
            window.location = "http://www.example.com/web/" + encodeURIComponent( $(this).find( '#s' ).val() );
            return false;
        });
    });
    </script>
    

    添加自定义重写规则,记得通过访问管理部分的settings-&gt;permalinks 选项卡来刷新规则。

    add_rewrite_rule( 'web/(.+?)/?$','index.php?s=$matches[1]' );
    

    【讨论】:

      猜你喜欢
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 2020-05-13
      • 2018-07-20
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      相关资源
      最近更新 更多