日安,
我想有一个特定的电子商务产品类别页面的自定义CSS,以排除侧边栏,并使该页面的内容完全宽度。现在,我可以让CSS在这些页面上工作,但我有450多个类别。是否有一种方法可以使被排除类别的术语id动态化?
到目前为止,我所掌握的脚本如下
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就更紧凑了,而且不会太长。如有任何建议,请
发布于 2022-07-29 10:34:50
@CBroe谢谢你为我指明了正确的方向。
我做的第一件事是制作一个代码片段,您可以将其复制并粘贴到您的子主题的functions.php页面中,或者使用类似于代码片段的插件。因此,当主题更新时,您不会丢失任何代码。
我所做的第一部分如下,我将这个CSS添加到页面的正文中。
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添加到代码段,以便仅在该页上显示代码。
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类添加到您指定的页面中,希望这有助于其他人。
https://stackoverflow.com/questions/73123275
复制相似问题