首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce如何从css中的多个段塞中获取术语id

Woocommerce如何从css中的多个段塞中获取术语id
EN

Stack Overflow用户
提问于 2022-07-26 12:18:35
回答 1查看 35关注 0票数 0

日安,

我想有一个特定的电子商务产品类别页面的自定义CSS,以排除侧边栏,并使该页面的内容完全宽度。现在,我可以让CSS在这些页面上工作,但我有450多个类别。是否有一种方法可以使被排除类别的术语id动态化?

到目前为止,我所掌握的脚本如下

代码语言:javascript
复制
if ( !is_product_category( array( 'cat1','cat3','cat4','cat6') ) ) {
add_action( 'wp_head', function () { ?>
<style>
 /*Hide SideBar*/
#primary {
     width: 100%!important; 
    border-right: 0px!important;
}
.term-1 .ast-right-sidebar #secondary,.term-3 .ast-right-sidebar #secondary,.term-4 .ast-right-sidebar #secondary,.term-6 .ast-right-sidebar #secondary{
    
    border-left: 0px!important
}
 .term-1.sidebar-main,.term-3 .sidebar-main,.term-4 .sidebar-main,.term-6 .sidebar-main {
    display:none!important
}
.term-1 #secondary,.term-3 #secondary,.term-4 #secondary,.term-6 #secondary {
    display: none!important;
    border-right: 0px solid #eee!important;
}
.term-1 .widget-area .secondary,.term-3 .widget-area .secondary,.term-4 .widget-area .secondary,.term-6 .widget-area .secondary
{
  display: none!important;

}
.term-1 .ast-right-sidebar #primary,.term-3 .ast-right-sidebar #primary,.term-4 .ast-right-sidebar #primary,.term-6 .ast-right-sidebar #primary {
    border-right: 0px solid #eee!important;
}
</style>
<?php } );
} 

我想动态地将-id这个词添加到CSS中,这样CSS就更紧凑了,而且不会太长。如有任何建议,请

EN

回答 1

Stack Overflow用户

发布于 2022-07-29 10:34:50

@CBroe谢谢你为我指明了正确的方向。

我做的第一件事是制作一个代码片段,您可以将其复制并粘贴到您的子主题的functions.php页面中,或者使用类似于代码片段的插件。因此,当主题更新时,您不会丢失任何代码。

我所做的第一部分如下,我将这个CSS添加到页面的正文中。

代码语言:javascript
复制
add_filter('body_class', 'add_custom_body_class');

function add_custom_body_class($classes){
   if(is_product_category(array( 'cat1','cat3','cat4','cat6'))){
     $classes[] = 'your_new_class_here';
   }
   return $classes;
}

然后,我创建了一个新的代码段,我将在其中将类添加到标题中,请记住,您不希望在每个页面上都有这个代码,所以我们还将将is_product_category添加到代码段,以便仅在该页上显示代码。

代码语言:javascript
复制
if ( !is_product_category( array( 'cat1','cat3','cat4','cat6') ) ) {
add_action( 'wp_head', function () { ?>
<style>
 /*Hide SideBar*/
body.your_new_class_here .ast-right-sidebar #secondary { border-left: 0px!important; } 
body.your_new_class_here .sidebar-main { display:none!important; }  
#primary {
     width: 100%!important; 
    border-right: 0px!important;
}
#secondary {
    display: none!important;
    border-right: 0px solid #eee!important;
}
</style>
<?php } );
}

它所做的是将CSS类添加到您指定的页面中,希望这有助于其他人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73123275

复制
相关文章

相似问题

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