Support » Themes and Templates » Editing functions.php in child theme

  • Resolved mcleek21


    I want to change the twentyeleven_body_classes so that I can get rid of .singular in my template that I made.

    I have made a new functions.php file in my child folder and I am having trouble overwriting twentyeleven_body_classes because it seems like the child functions.php file is called before the parent. If that is the case I am not sure how to remove a filter and add mine. I basically just want to add ‘&& ! is_page_template( ‘my-new-template.php’ )’ into the if statment so that my new template is exempt from getting the .singular class added to it.


Viewing 5 replies - 1 through 5 (of 5 total)
  • remove_filter( ‘body_class’, ‘twentyeleven_body_classes’ );

    Thanks Andrew,

    Does this go in my child theme functions.php file? It doesn’t seem to work for me. My functions.php file in its entirety:

    function twentyeleven_child_body_classes( $classes ) {
    	if ( function_exists( 'is_multi_author' ) && ! is_multi_author() )
    		$classes[] = 'single-author';
    	if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) && ! is_page_template( 'sidebar-page.php' ) && ! is_page_template( 'my-new-template.php' ) )
    		$classes[] = 'singular';
    	return $classes;
    remove_filter( 'body_class', 'twentyeleven_body_classes' );
    add_filter( 'body_class', 'twentyeleven_child_body_classes' );

    I tried putting remove_filter() before the function, and after the add as well. My code doesn’t break, but it is not overwriting body_class either.

    Yea you can’t just paste it in because your child’s functions.php gets loaded before twentyeleven’s functions.php.

    Here’s a good article on overriding parent theme functionality.

    WordPress PROTIP: Child Themes

    Since twentyeleven_body_classes() isn’t pluggable you’ll have use add_action(‘after_setup_theme’,’yourfunction’).

    If you give it a try for a while and still haven’t got it figured out I’ll give you some copy/paste code but it’ll be more rewarding in the long run to learn this process yourself.

    Happy coding!

    Thank you good sir.

    That is what I needed. I knew the child functions.php was called before so I wasn’t sure how to get my functions to overwrite the other. To remove the body_class from functions in twentyeleven I used

    function remove_twentyeleven_body_classes(){
    	remove_filter('body_class', 'twentyeleven_body_classes');
    add_action('init', 'remove_twentyeleven_body_classes');

    Thanks for your help.

    Thank you for this solution!
    Any particular reason why twentyeleven_body_classes function is not wrapped with an if statement, like the rest of the functions?

    if ( ! function_exists( 'twentyeleven_body_classes' ) ) :
     // Code here
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Editing functions.php in child theme’ is closed to new replies.