How do I override template-tags.php in twentyseventeen theme

I created a child theme from the TwentySeventeen theme and it seems to work.

I can override the content.php file by creating a matching folder and my own content.php in my child theme.

/template-parts/post/content.php

…Works fine.

However, I tried to do the same thing to override the metta tags in the header of a single post by doing the following

/inc/template-tags.php

…and the functions in template-tags.php (eg. twentyseventeen_posted_on() ) are -not- overriding the main theme’s version as I expected.

What am I doing wrong?

1 Answer
1

If you take a look at template-tags.php file, then you’ll see something like that:

if ( ! function_exists( 'twentyseventeen_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
 */
function twentyseventeen_posted_on() {

    // Get the author name; wrap it in a link.
    $byline = sprintf(
        /* translators: %s: post author */
        __( 'by %s', 'twentyseventeen' ),
        '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . get_the_author() . '</a></span>'
    );

    // Finally, let's write all of this to the page.
    echo '<span class="posted-on">' . twentyseventeen_time_link() . '</span><span class="byline"> ' . $byline . '</span>';
}
endif;

The if statement in the first line of that code is responsible for checking if given function is already defined.

So what you can do about it? Just define your own function called twentyseventeen_posted_on in your child theme and your function will be the one that is used.

Important: It’s crucial that you define your function before TwentySeventeen defines it’s own version.

Leave a Comment