Support » Theme: Twenty Seventeen » Customizing /inc/template-functions.php in Child Theme

  • Resolved hopetommola

    (@hopetommola)


    Customizing /inc/template-functions.php in the child theme is not overriding the parent theme.

    We are trying to remove the sidebar (i.e. not add has-sidebar class to the body) on a single page for a custom post type. Adding && ! is_singular('staff') to line 43 in the parent theme’s /inc/template-functions.php file works. But making the same change in a child theme’s /inc/template-functions.php does not work, nor does copying the function to the child theme’s functions.php file.

Viewing 5 replies - 1 through 5 (of 5 total)
  • waldbach

    (@waldbach)

    I have the same issue. But I am trying to have the header size of the frontpage be the same as on any other page, wanted to change the class ‘twentyseventeen-front-page’ to another class, but it doesn’t override this line:

    
    // Add class on front page.
    	if ( is_front_page() && 'posts' !== get_option( 'show_on_front' ) ) {
    		$classes[] = 'twentyseventeen-front-page';
    	}
    

    So, how do we override settings in this file?

    • This reply was modified 3 years ago by waldbach.
    David A. Kennedy

    (@davidakennedy)

    Hi @hopetommola,

    Good question. This can be tricky to understand at first.

    The reason simply overriding in a child theme doesn’t work is because a child theme’s functions.php run first, then the parent theme’s functions.php. So in your case, Twenty Seventeen’s twentyseventeen_body_classes() still runs, overriding your changes.

    You need to pay attention to a filter hook’s priority. See this article.

    That can be changed to make sure your function runs after another function hooked to the same filter. Check out this code, with comments explaining it.

    /**
     * Adds and/or removes custom classes to the array of body classes.
     * Uses 11 priority to run after parent theme. Default is 10.
     *
     * @param array $classes Classes for the body element.
     * @return array
     */
    function childtheme_body_classes( $classes ) {
    	// Removes has-sidebar class from custom post type.
    	// If the sidebar is active and a single post of my-post-type is viewed.
    	if ( is_active_sidebar( 'sidebar-1' ) && is_singular( 'my-post-type' ) ) {
    		// If the has-sidebar class is in the $classes array, do some stuff.
    		if ( in_array( 'has-sidebar', $classes ) ) {
    			// Remove the class.
    			unset( $classes[ array_search( 'has-sidebar', $classes ) ] );
    		}
    	}
    
    	// Give me my new, modified $classes.
    	return $classes;
    }
    add_filter( 'body_class', 'childtheme_body_classes', 11 );

    This way, only the part of the function you need to modify is changed in the child theme, and the parent theme does the rest.

    Make sense?

    hopetommola

    (@hopetommola)

    I see. So essentially, any of the /inc files from the parent theme will still be pulled in from the parent theme, since the last lines of the parent functions.php file runs after the child theme’s functions.php file. And because of this, creating a /child-theme/inc/template-functions.php file is essentially useless. Correct?

    Thank you!

    hopetommola

    (@hopetommola)

    Got it! Thanks for the great explanation. I originally tried to exclude my post type from having the class added, but now understand that because the order the functions are run, I have to remove the class.

    Thanks so much!

    (still find it confusing why the template functions are in a separate file, not just in functions.php, but I’m sure there’s a lengthy trac debate somewhere that resulted in this decision)

    David A. Kennedy

    (@davidakennedy)

    @hopetommola

    I see. So essentially, any of the /inc files from the parent theme will still be pulled in from the parent theme, since the last lines of the parent functions.php file runs after the child theme’s functions.php file. And because of this, creating a /child-theme/inc/template-functions.php file is essentially useless. Correct?

    Right, those parent theme functions will run. Simply overriding the file doesn’t make them not run. Glad you got it.

    (still find it confusing why the template functions are in a separate file, not just in functions.php, but I’m sure there’s a lengthy trac debate somewhere that resulted in this decision)

    This is mainly to break things up and not have the functions.php file be really long.

    functions.php has default/Core functionality.

    template-functions.php has additional features to allow styling of the templates. Mostly behind the scenes stuff.

    template-tags.php has custom template tags for the theme. This is actual markup rendered on the front end.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Customizing /inc/template-functions.php in Child Theme’ is closed to new replies.