首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修改WooCommerce搜索结果?

如何修改WooCommerce搜索结果?
EN

Stack Overflow用户
提问于 2019-07-07 19:29:52
回答 2查看 9.4K关注 0票数 0

我估计WooCommerce搜索使用了像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

代码语言:javascript
复制
<?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="<?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();

我该如何实现这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-07 22:38:05

首先,确保您在wp-content/themes/yourtheme/woocommerce文件夹下有一份archive-product.php副本。您可以从wp-content/plugins/woocommerce/templates复制该文件

在archive-product.php中,您必须将搜索结果与商店页面中的搜索结果区分开来,因为两者共享相同的模板。使用来自WooCommerce search result template的below..taken之类的东西

代码语言:javascript
复制
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模板复制到您的主题文件夹中),在其中您可以定制您的搜索结果。您可以通过将新函数放在主题文件夹下的functions.php中来覆盖钩子,如"woocommerce_before_shop_loop_item“。

票数 1
EN

Stack Overflow用户

发布于 2021-03-31 12:05:23

这很简单,将以下代码添加到functions.php文件中:

代码语言:javascript
复制
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' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56921750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档