为搜索页面过滤器添加自定义CSS类

时间:2020-07-22 08:17:47

标签: php wordpress custom-wordpress-pages wordpress-hook

我正在尝试为使用WooCommerce产品搜索的搜索页面添加自定义CSS类。

搜索页面路径类似于:

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

当我搜索代表产品标签的数字3时。

我设法通过以下功能添加了整个商店的类,但我只想为搜索页面添加类。有人可以帮我吗?

add_filter( 'body_class', 'add_body_classes' );

function add_body_classes( $classes ) {

   global $post;

     if ( is_shop() ) 

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

}

2 个答案:

答案 0 :(得分:0)

请更新以下代码

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

答案 1 :(得分:0)

假设搜索页面正在使用archive-product.php

如果您使用兼容WooCommerce的主题,则必须进入themes folder -> woocommerce并找到archive-product.php。如果替代文件不存在,请转到wp-content/plugins/woocommerce/templates/archive-product.php

还有一个if语句,用于检查是否为搜索条件,如下所示:

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

*如果零件(在您的情况下为body类)不存在,则必须找到该零件并进行相同的操作(可能在您的header.php中)

相关问题