虽然上面的解决方案使我能够在通过body\\u class()添加类的情况下将一些有用的东西组合在一起,但我遇到了另一种情况,因为这些类是通过JavaScript添加的,所以它们都不起作用。
此外,我只需要删除特定页面上的类,无法通过修改或重写JS来完成这一操作,而不会影响其他页面。
为了解决这个问题,我在函数中添加了以下内容。子主题的php。。。
<?php
// remove body classes not added through the wp body_class() function
// jQuery solution
function cbc_manage_body_classes() {
$to_remove = \'\';
if (is_shop()) { $to_remove .= \'class_one class_two\'; }
if ( ! empty($to_remove)) {
?>
<script type="text/javascript">
jQuery(document).ready(function(){ jQuery("body").removeClass(\'<?php echo $to_remove; ?>\'); });
</script>
<?php
}
}
add_action(\'wp_footer\',\'cbc_manage_body_classes\', 99);
上述jQuery解决方案对我的用例非常有效。对于那些需要/想要一个纯JavaScript解决方案的人,我将其放在一起。。。
<?php
// remove body classes not added through the wp body_class() function
// Javascript only solution
function cbc_manage_body_classes() {
$to_remove = \'\';
if (is_shop()) { $to_remove .= \'class_one class_two\'; }
if ( ! empty($to_remove)) {
?>
<script type="text/javascript">
window.onload = function() {
// inspiration from: https://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array
function cbcInArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) { if(haystack[i] == needle) return true; }
return false;
}
// inspiration from: https://stackoverflow.com/questions/13505253/how-to-remove-the-class-in-javascript
function cbcRemoveClass( elem, name ) {
var classlist = elem.className.split( /\\s/ ),
namelist = name.split( /\\s/ ),
newlist = [],
idx = 0;
for ( ; idx < classlist.length; idx++ ) {
if ( ! cbcInArray(classlist[ idx ], namelist) ) { newlist.push( classlist[ idx ] ); }
}
elem.className = newlist.join(" ");
return true;
}
cbcRemoveClass(document.getElementsByTagName("body")[0], \'<?php echo $to_remove; ?>\');
};
</script>
<?php
}
}
add_action(\'wp_footer\',\'cbc_manage_body_classes\', 99);
?>
为了与大家分享,下面是我为body\\u class()用例所做的工作,使用了我之前发布的想法。
此代码与发布的snips的主要区别在于,此代码同时启用白名单和黑名单。
在我为构建这个的情况下,我需要将body类全局限制为特定的集,然后删除特定页面上的其他类。
<?php
//inpsired by: https://wordpress.stackexchange.com/questions/15850/remove-classes-from-body-class
function cbc_body_class_filter( $wp_classes, $extra_classes ) {
// merge $wp_classes and $extra_classes so all classes are subjected to the filtering
$classes = array_merge( $wp_classes, (array) $extra_classes );
// init list(s) of classes (not)allowed
$whitelist = $blacklist = array();
// block/remove body classes on wc shop page
// this block can be repeated for different pages
if (is_shop()) { // is_page(\'PAGE SLUG/NAME/ID\')) {
$blacklisted = array(\'class-one\', \'class-two\');
$blacklist = array_merge($blacklist, $blacklisted);
}
// filter the body classes through the lists
// by filtering the whitelist first we can create a "master" whitelist and then remove other classes on a page by page basis
if ( ! empty($whitelist)) { $classes = array_intersect( $classes, $whitelist ); }
if ( ! empty($blacklist)) { $classes = array_diff( $classes, $blacklist ); }
return $classes;
}
add_filter( \'body_class\', \'cbc_body_class_filter\', 99, 2 );
?>