简体   繁体   中英

WordPress Amend Body Class

I want to add -wp to the beginning of ALL the body classes which WordPress adds using http://codex.wordpress.org/Function_Reference/body_class

The reason for this is because I sometimes want to use classes like .attachment or .page from the root of the CSS document and as it is the body class it clashes.

The codex shows me how to add classes to the body but not how I can add to each individual class it adds.

You can do this by adding an action on body_class hook.

add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
  // You have all the classes in $classes
  // Iterate and add the wp prefix.
  $new_classes = array();
  foreach($classes as $cls) {
    $new_classes[] = 'wp-' . $cls;
  }

   return $new_classes;
}

The code is not tested, but it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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