我认为WooCommerce搜索使用的是类似http://localhost/myshoesssite/?s=noheelshoes&post_type=product的URL
因此,我需要编辑搜索结果,以便可以在每个产品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="<?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();
我该如何实现?
答案 0 :(得分:0)
首先,请确保在wp-content / themes / yourtheme / woocommerce文件夹下有archive-product.php的副本。您可以从wp-content / plugins / woocommerce / templates复制该文件
在archive-product.php中,您必须区分搜索结果和商店页面中的搜索结果,因为两者共享相同的模板。使用类似下面的内容..摘自WooCommerce search result template
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 :(得分:0)
很简单,将此代码添加到functions.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' );