简体   繁体   English

为搜索页面过滤器添加自定义 css class

[英]Add custom css class for the search page filter

I'm trying to add a custom CSS class for the search page that uses WooCommerce Product Search.我正在尝试为使用 WooCommerce 产品搜索的搜索页面添加自定义 CSS class。

Search pages path is like:搜索页面路径如下:

domain.com/shop/ixwpss=3&title=0&excerpt=0&content=0&categories=0&attributes=0&tags=1&sku=0&v=a3420e5a4c03 

When I search for the number 3 that represents the tag for products.当我搜索代表产品标签的数字 3 时。

I managed to add the class for the whole shop, with the following function but I want to add only for the search page.我设法为整个商店添加了 class,下面是 function 但我只想为搜索页面添加。 Can someone help me?有人能帮我吗?

add_filter( 'body_class', 'add_body_classes' );

function add_body_classes( $classes ) {

   global $post;

     if ( is_shop() ) 

     $classes[] = 'search'; 
   
   return $classes;

}

Please update the code like below请更新如下代码

add_filter( 'body_class', function( $classes ) { 
 if ( is_shop() ) {return array_merge( $classes, array( 'class-name' ) );}
}

Assuming that the search page is using the archive-product.php假设搜索页面使用的是archive-product.php

If you are using a WooCommerce compatible theme then you have to go in your themes folder -> woocommerce and find the archive-product.php .如果您使用的是与 WooCommerce 兼容的主题,那么您必须在themes folder -> woocommerce并找到archive-product.php 。ZE09634FD76206DE841E If the override file does not exists then go to wp-content/plugins/woocommerce/templates/archive-product.php如果覆盖文件不存在,则 go 到wp-content/plugins/woocommerce/templates/archive-product.php

And a if statement to check if is search or not like below:还有一个 if 语句来检查是否是搜索,如下所示:

if ( is_search() ) {
    <body class="your-class">
} else {
    <body class="regular-class">
    // the content that is already in that file (archive-product.php)
}

*if the part (body class in your case) is not there you have to locate it and do the same (probably it's in your header.php ) *如果零件(在您的情况下为主体 class)不存在,您必须找到它并执行相同操作(可能在您的header.php中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM