【问题标题】:WooCommerce Search Form From Revolution Slider来自革命滑块的 WooCommerce 搜索表单
【发布时间】:2017-08-22 03:39:38
【问题描述】:

我正在使用以下内容将搜索表单放置在滑块革命英雄幻灯片中。但是它会搜索整个站点。我希望它只搜索 woocommerce 产品。有什么想法我需要改变才能使其发挥作用吗?

<form role="search" method="get" id="searchform" class="revtp-searchform" action="http://mywebsiteURL.com/"/>
    <input type="text" value="" name="s" id="s" placeholder="What are you looking for?" />
    <input type="submit" id="searchsubmit" value="Find" />
</form>

【问题讨论】:

    标签: wordpress forms search woocommerce


    【解决方案1】:

    您可以使用codex: pre_get_postsdeveloper: pre_get_posts 解决它

    add_action( 'pre_get_posts', 'search_woocommerce_product' );
    
    function search_woocommerce_product( $query ) {
      if( ! is_admin() && is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', 'product' );
      }
    }
    

    您可以向此钩子函数添加更多条件,例如仅在特定页面中包含 is_page

    或者像这样的某些短代码

    $pattern = get_shortcode_regex();
    $patternToken = 'masterslider';
    
    if (preg_match_all('/' . $pattern . '/s', get_the_content(), $matches) &&
    array_key_exists(2, $matches) && 
    in_array($patternToken, $matches[2])) {
    //Your code logic
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多