Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the theme twentyfouteen, the full-width class is defined with the following code

if ( ( ! is_active_sidebar( 'sidebar-2' ) )
|| is_page_template( 'page-templates/full-width.php' )
 || is_page_template( 'page-templates/contributors.php' )
 || is_attachment() ) {
 $classes[] = 'full-width';
 }

from this function

function twentyfourteen_body_classes( $classes ) {
if ( is_multi_author() ) {
    $classes[] = 'group-blog';
}

if ( get_header_image() ) {
    $classes[] = 'header-image';
} else {
    $classes[] = 'masthead-fixed';
}

if ( is_archive() || is_search() || is_home() ) {
    $classes[] = 'list-view';
}

if ( ( ! is_active_sidebar( 'sidebar-2' ) )
    || is_page_template( 'page-templates/full-width.php' )
    || is_page_template( 'page-templates/contributors.php' )
    || is_attachment() ) {
    $classes[] = 'full-width';
}

if ( is_active_sidebar( 'sidebar-3' ) ) {
    $classes[] = 'footer-widgets';
}

if ( is_singular() && ! is_front_page() ) {
    $classes[] = 'singular';
}

if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
    $classes[] = 'slider';
} elseif ( is_front_page() ) {
    $classes[] = 'grid';
}

return $classes;
}
add_filter( 'body_class', 'twentyfourteen_body_classes' );

I'm adding a selection of sidebars to the sidebar-content.php file. These sidebars must also have the same body class as sidebar-2. Is it correct for me to just use a different fuction name as to that of the twentyfourteen theme and to still hook on the body_class, keep the same class name (full-width), and change the above code in a child theme to

if ( ( ! is_active_sidebar( 'sidebar-2' ) &&  ! is_active_sidebar( $sidebar_option ) )
 || is_page_template( 'page-templates/full-width.php' )
 || is_page_template( 'page-templates/contributors.php' )
|| is_attachment() ) {
 $classes[] = 'full-width';
 }

I'm getting a lot of stuff that tells me how to ad a new body class and removing one, but nothing on how to change an existing body class of a parent theme in a child theme or if it is even possible

share|improve this question
1  
There is nothing wrong with you creating a new function and also hooking it to body_class. If you don't want the parent theme's function you can also unhook that. –  helgatheviking Jan 30 at 13:27

2 Answers 2

up vote 3 down vote accepted

Thank you for all the insets with this question. Here is what I come up with working from 1fixdotio's answer above. I first had to unset the full-width body class and then register a new body class to incorporate my new sidebar. Here is the code

  // Remove body class and register new body class to accomodate the new custom-sidebar-per-page sidebars
function remove_a_body_class($classes) {
      foreach($classes as $key => $value) {
      if ($value == 'full-width') unset($classes[$key]);
      }
return $classes;
}

add_filter('body_class', 'remove_a_body_class', 20, 2);

function pietergoosen_add_new_body_class( $classes ) {
$options = get_post_custom($post->ID);  
$sidebar_choice = $options['custom_sidebar_per_page'][0];  
    if ( (! is_active_sidebar( 'sidebar-2' ) && ! is_active_sidebar( $sidebar_choice ) )
        || is_page_template( 'page-templates/full-width.php' )
        || is_page_template( 'page-templates/contributors.php' )
        || is_attachment() ) {
        $classes[] = 'custom-sidebar-per-page-width';
    }

    return $classes;
}
add_filter( 'body_class', 'pietergoosen_add_new_body_class' );
share|improve this answer
1  
Cool! Not sure if I still need to edit my code accordingly... But glad you figured it out by yourself. And feel free to mark your own answer as accepted, so people will skip my answer. –  1fixdotio Jan 30 at 18:39

Let's say if you don't like the default page body class which comes with WP by default, you could do this in your filter function:

if ( ( ! is_active_sidebar( 'sidebar-2' ) &&  ! is_active_sidebar( $sidebar_option ) )
 || is_page_template( 'page-templates/full-width.php' )
 || is_page_template( 'page-templates/contributors.php' )
|| is_attachment() ) {
    if(($key = array_search( 'page', $classes)) !== false) {
        unset($classes[$key]); // Unset what you don't need
    }
    $classes[] = 'full-width'; // Add what you want
}

Or you could do this if you want to remove ALL body classes come with WP:

if ( ( ! is_active_sidebar( 'sidebar-2' ) &&  ! is_active_sidebar( $sidebar_option ) )
 || is_page_template( 'page-templates/full-width.php' )
 || is_page_template( 'page-templates/contributors.php' )
|| is_attachment() ) {
    $classes = array( 'full-width' ); // Don't use default array, set a new one
}

Hope I didn't get you wrong.

share|improve this answer
    
This is incomplete, please wrap it up with a function and add the proper hooking. just keep in mind people will find this answer from google and will not have the context the OP probably has. –  Mark Kaplun Jan 30 at 17:41
    
Not sure if I should do as you expected. I just follow @Pieter's context, adding extra code might cause him confused. I'll let him decide if this answer would work. Anyway, thanks for your reminding. –  1fixdotio Jan 30 at 18:07
    
many people copy paste code, please help them. you and me also do it from time to time. –  Mark Kaplun Jan 30 at 18:11
    
I will paste my answer that I came up with working from 1fixdotio answer. –  Pieter Goosen Jan 30 at 18:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.