Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Hooks - Actions and Filters Action Functions in WordPress The do_action Function

Fernando Cortes
Fernando Cortes
2,435 Points

Displaying custom footer in custom post pages

I'm still finding this quite hard to apply in real life.

In my child theme, I would like to have a different footer display in a custom post type. How should I go about it? I've tried several things, but I keep getting errors or no changes.

Some of the questions I have are:

  • do I need to create a footer-customname.php template? I've done that, but I don't know if I should, since I can't find how to pull this template at the right place for it to display.

  • what code should I use and where in my different files? If I'm understanding it correctly, WPress comes with a hook for this, in the wp-includes/generate-template.php file?

<?php
function get_footer( $name = null ) {
    /**
     * Fires before the footer template file is loaded.
     *
     * The hook allows a specific footer template file to be used in place of the
     * default footer template file. If your file is called footer-new.php,
     * you would specify the filename in the hook as get_footer( 'new' ).
     *
     * @since 2.1.0
     * @since 2.8.0 $name parameter added.
     *
     * @param string $name Name of the specific footer file to use.
     */
    do_action( 'get_footer', $name );

    $templates = array();
    $name = (string) $name;
    if ( '' !== $name )
        $templates[] = "footer-{$name}.php";

    $templates[] = 'footer.php';

    // Backward compat code will be removed in a future release
    if ('' == locate_template($templates, true))
        load_template( ABSPATH . WPINC . '/theme-compat/footer.php');
}
?>

Or is this not at all what I should be using? I've copied the above code into my child theme's functions.php, replacing $name with the 'customname' slug of my footer-customname.php template, then called it (I've tried different ways and places) with get_footer ( 'customname' ); but it's not working.

Clearly, I'm missing something. Could anybody please help me understand?

Fernando Cortes
Fernando Cortes
2,435 Points

Ok, just a clarification and an update to my question.

The footer-customname.php template I created was actually being pulled up. I was just not seeing it because I was looking in the wrong place. The content I was trying to customize was not in the page footer, but in one of the post's divs; it had a " .something-footer" class and that got me confused.

Since the template was being pulled without changing anything else in my child theme, I conclude that's just how WordPress works. If you have a single-customname.php and a footer-customname.php, the latter is automatically pulled when get_footer() is called from the former.

I still don't know if creating a custom footer template for a child-theme is good practice, and wonder if the WordPress hook I posted in my original question has any relevance to this, since I didn't even have to use

get_footer('customname');

in the single-customname file for the footer-customname.php template to be pulled.