จะแก้ไขผลการค้นหา WooCommerce ได้อย่างไร?

ฉันคิดว่าการค้นหา WooCommerce ใช้ URL เช่น http://localhost/myshoesssite/?s=noheelshoes&post_type=product

ดังนั้น ฉันจึงต้องแก้ไขผลการค้นหาเพื่อให้สามารถเพิ่มพารามิเตอร์ URL ที่ส่วนท้ายของ URL ผลิตภัณฑ์แต่ละรายการได้ เช่น http://localhost/myshoesssite/product/no-heel-shoes?get=specs โดยที่ get=specs คือพารามิเตอร์ ฉันเพียงต้องการเพิ่มพารามิเตอร์นี้ในผลการค้นหาของฉัน ไม่ใช่ใน URL ผลิตภัณฑ์ทั่วไปของฉัน

ฉันลองใช้ search.php ในธีมของฉัน แต่ดูเหมือนว่าเมื่อฉันแก้ไขไฟล์ ผลการค้นหาจะไม่เปลี่ยนแปลง ด้านล่างคือ search.php ของฉัน

<?php
/**
 * The template for displaying search results pages.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * 
 */

get_header(); ?>

<div id="primary" class="site-content content-wrapper topofset">
    <div id="main" class="container" >
        <div class="content content_search" >
            <?php
            if ( have_posts() ) : ?>

                <header class="page-header">
                    <h1 class="page-title text-light"><?php printf( esc_html__( 'Search Results for: %s', 'myticket' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                </header><!-- .page-header -->

                <?php
                /* Start the Loop */
                while ( have_posts() ) : the_post(); ?>

                     <span class="search-post-title">Testing<?php the_title(); ?></span>
            <span class="search-post-excerpt"><?php the_excerpt(); ?></span>
            <span class="search-post-link"><a href="/th<?php the_permalink(); ?>"><?php the_permalink(); ?></a></span>


                    <?php
                    /**
                     * Run the loop for the search to output the results.
                     * If you want to overload this in a child theme then include a file
                     * called content-search.php and that will be used instead.
                     */
                    //get_template_part( 'template-parts/content', 'search' );

                endwhile;

            else :

                get_template_part( 'template-parts/content', 'none' );

            endif; ?>
        </div>
    </div><!-- #main -->
</div><!-- #primary -->

<?php
get_footer();

ฉันจะบรรลุเป้าหมายนี้ได้อย่างไร?


person TTOmoon    schedule 07.07.2019    source แหล่งที่มา


คำตอบ (2)


ประการแรก ตรวจสอบให้แน่ใจว่าคุณมีสำเนาของ archive-product.php อยู่ในโฟลเดอร์ wp-content/themes/yourtheme/woocommerce คุณสามารถคัดลอกไฟล์นั้นได้จาก wp-content/plugins/woocommerce/templates

ภายใน archive-product.php คุณต้องแยกแยะผลการค้นหาของคุณระหว่างผลการค้นหาที่ปรากฏกับการค้นหาและผลการค้นหาที่ปรากฏในหน้าร้าน เนื่องจากทั้งสองใช้เทมเพลตเดียวกัน ใช้สิ่งที่ต้องการด้านล่าง..นำมาจากเทมเพลตผลการค้นหา WooCommerce

if ( is_search() ) {
    //put your search results markup here (you can copy some code from archive-product.php file and also from content-product.php to create a standard markup
} else {
    // here goes the content that is already in that file (archive-product.php) 
}

อยู่ในคำสั่ง is_search() if ที่คุณแก้ไขผลการค้นหาตามที่คุณต้องการ คุณสามารถสร้างสำเนาของ content-product.php อีกชุด (อีกครั้งโดยคัดลอกจากเทมเพลต woocommerce ไปยังโฟลเดอร์ธีมของคุณ) และภายในนั่นคือที่ที่คุณจะได้ปรับแต่งผลการค้นหาของคุณ คุณสามารถแทนที่ hooks เช่น "woocommerce_before_shop_loop_item" ได้โดยการวางฟังก์ชันใหม่ของคุณใน Functions.php ใต้โฟลเดอร์ธีมของคุณ

person Melvin Wong    schedule 07.07.2019

ง่ายๆ เพียงเพิ่มโค้ดนี้ลงในไฟล์ function.php:

function after_setup_theme_7cloner_com() {
    add_theme_support(
        'html5',
        array(
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
            'style',
            'script',
            'navigation-widgets',
        )
    );

    add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'after_setup_theme_7cloner_com' );
person 7Cloenr    schedule 31.03.2021
comment
ยินดีต้อนรับสู่ Stack Overflow! แม้ว่าโค้ดนี้อาจช่วยแก้ปัญหาได้ รวมถึงคำอธิบายว่าเหตุใดจึงแก้ปัญหาได้จริง แต่จะช่วยปรับปรุงคุณภาพโพสต์ของคุณได้จริงๆ และอาจส่งผลให้มีการโหวตมากขึ้น จำไว้ว่าคุณกำลังตอบคำถามของผู้อ่านในอนาคต ไม่ใช่แค่คนที่ถามในตอนนี้ โปรดแก้ไขคำตอบของคุณเพื่อเพิ่มคำอธิบายและระบุว่ามีข้อจำกัดและสมมติฐานใดบ้าง - person cigien; 05.04.2021