0

I have the following parent theme function located in inc/template-tags.php

    if ( ! function_exists( 'twentysixteen_excerpt' ) ) :
    /**
     * Displays the optional excerpt.
     *
     * Wraps the excerpt in a div element.
     *
     * Create your own twentysixteen_excerpt() function to override in a child theme.
     *
     * @since Twenty Sixteen 1.0
     *
     * @param string $class Optional. Class string of the div element. Defaults to 'entry-summary'.
     */
    function twentysixteen_excerpt( $class = 'entry-summary' ) {
        $class = esc_attr( $class );

        if ( has_excerpt() || is_search() ) :
            ?>
            <div class="<?php echo $class; ?>">
                <?php the_excerpt(); ?>
            </div><!-- .<?php echo $class; ?> -->
            <?php
        endif;
    }
endif;

however it doesn't wrap my child theme posts output in divs. I know that if a child theme doesn't have function with same name, parent function should kick in. I am wondering however if my parent function not working because of a different folder structure in my child theme - I don't have inc/template-tags.php

Interestingly enough I was able to override in my child theme the following parent function located in the same file inc/template-tags.php . I placed the below function in my child theme functions.php and changed the wording of "Continue reading" to "Read more" which took effect in the child theme.

So my question is why the first function has no effect on child theme while the second does?

  if ( ! function_exists( 'twentysixteen_excerpt_more' ) && ! is_admin() ) :
    /**
     * Replaces "[...]" (appended to automatically generated excerpts) with ... and
     * a 'Continue reading' link.
     *
     * Create your own twentysixteen_excerpt_more() function to override in a child theme.
     *
     * @since Twenty Sixteen 1.0
     *
     * @return string 'Continue reading' link prepended with an ellipsis.
     */
    function twentysixteen_excerpt_more() {
        $link = sprintf(
            '<a href="%1$s" class="more-link">%2$s</a>',
            esc_url( get_permalink( get_the_ID() ) ),
            /* translators: %s: Post title. */
            sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentysixteen' ), get_the_title( get_the_ID() ) )
        );
        return ' &hellip; ' . $link;
    }
    add_filter( 'excerpt_more', 'twentysixteen_excerpt_more' );
endif;
  • Regardless your child theme structure, a parent function is always called (where & when necessary, of course), but if your child theme overrides the function, then your child theme's function would be used instead. And that first function is used to display automatic excerpt only, so if you have used a custom/manual excerpt, then the function does nothing except on search result pages. – Sally CJ May 22 at 22:50
  • Hi Sally CJ, thank you for your response. I just tested the first function by adding manual custom excerpt and it did show it wrapped in divs. I guess you meant that first function would only show manual excerpts if either condition is true - has_excerpt() (post has manual excerpt) || is_search() ( search query) – 810311 May 23 at 4:02
  • Yes, but more precisely, on search result pages/requests (where is_search() returns true), the function would always show the post excerpt be it custom/manual or automatic. On all other pages where the function is being called, the function only shows manual excerpt if it's available (i.e. has_excerpt() returns true). – Sally CJ May 24 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.