【问题标题】:building woocommerce search results page from scratch - search term ignored从头开始构建 woocommerce 搜索结果页面 - 搜索词被忽略
【发布时间】:2018-10-10 15:23:15
【问题描述】:

我正在使用自定义构建的主题,但在构建搜索页面时遇到了问题。

主题基于 HTML Bones 项目 - https://github.com/eddiemachado-zz/bones

我已经设法让搜索页面显示循环,但它没有使用提供的搜索词,而是显示所有产品。

我希望有更多经验的人能够指出为什么在此设置中忽略了搜索词?

搜索表单的代码是:

<form role="search" method="get" id="searchform" class="searchform" action="<?php echo site_url( '/' ); ?>">
    <div>
        <label for="s" class="screen-reader-text"><?php _e('Search for:','bonestheme'); ?></label>
        <input type="search" id="s" name="s" value="" />

        <button type="submit" id="searchsubmit" ><?php _e('Search','bonestheme'); ?></button>
    </div>
</form>

结果页面代码为:

<?php
/**
 * The template for displaying search results pages.
 *
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="container site-main" role="main">

        <ul id="product-list-global" class="products product-list-global col-sm-9">
    <?php

        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();

                wc_get_template_part( 'content', 'product' );

            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul><!--/.products-->
            <?php
 get_template_part( 'sidebar-shop-default' ); ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php

get_footer();

感谢阅读!

【问题讨论】:

    标签: php wordpress loops search woocommerce


    【解决方案1】:

    在function.php中添加下面的代码

    add_filter( 'get_product_search_form' , 'woo_custom_product_searchform' );
    
    /**
     * woo_custom_product_searchform
     *
     * @access      public
     * @since       1.0 
     * @return      void
    */
    function woo_custom_product_searchform( $form ) {
    
        $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/'  ) ) . '">
            <div>
                <label class="screen-reader-text" for="s">' . __( 'Search for:', 'woocommerce' ) . '</label>
                <input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __( 'My Search form', 'woocommerce' ) . '" />
                <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search', 'woocommerce' ) .'" />
                <input type="hidden" name="post_type" value="product" />
            </div>
        </form>';
    
        return $form;
    
    }
    

    实际上,您可以在 archive-product.php 中使用条件 is_search() 代替,search.php 文件仅用于全局搜索,并且还从结果代码中删除 $args 和循环,因为默认情况下 woocommerce 提供搜索模板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      相关资源
      最近更新 更多