Get WPFTS Pro today with 50% discount!

[Solved] No excerpts shown in search results in Storefront theme



  • Another good and clean theme (and it's also free) called Storefront which not show excerpts in search results neither for posts and pages nor for files. This way, search for files by their content works well, but the Storefront theme only displays titles (filenames).

    Usual and the simplest way to fix this is to redefine the search.php template in the child theme like we did it for the Thrive Nouveau theme. But may we achieve that without creating the child theme? If you downloaded the theme from the official WP repository, it has not child theme in the package.

    After the fast analyze of the theme, I found that the excerpt is generated by the function storefront_post_content located in the file inc/storefront-template-functions.php and it uses the the_content() call to show both full-content and short content of posts. You may check WP documentation to understand how it works.

    But this method is not acceptable for us, because WPFTS Pro catches and uses the the_excerpt() hook to show its famous Smart Excerpt. So what I gonna do is to replace this function completely.

    I don't need to remove the existing function from the theme, because the theme only creates this function if it was not defined before. So what I need to do is to DEFINE my function before the theme.

    I going to create a simple plugin and put this code into it.

    /**
     * This code will be called BEFORE the Storefront's original code
     */
    if (!function_exists('storefront_post_content')) {
    
        function storefront_post_content()
        {
            global $wp_query;
    
            ?>
            <div class="entry-content">
                <?php
                    /**
                     * Functions hooked in to storefront_post_content_before action.
                     *
                     * @hooked storefront_post_thumbnail - 10
                     */
                    do_action( 'storefront_post_content_before' );
    
                    if ($wp_query && $wp_query->is_main_query() && $wp_query->is_search) {
                        // We are using the_excerpt() for search result always!
                        the_excerpt();
                    } else {
                        // We are using old method for all other pages
                        the_content(
                            sprintf(
                                __( 'Continue reading %s', 'storefront' ),
                                    '<span class="screen-reader-text">' . get_the_title() . '</span>'
                                )
                            );
                    }
    
                    do_action( 'storefront_post_content_after' );
    
                    wp_link_pages(
                        array(
                            'before' => '<div class="page-links">' . __( 'Pages:', 'storefront' ),
                            'after'  => '</div>',
                        )
                    );
                    ?>
                    </div><!-- .entry-content -->
                    <?php	
             }
    }
    

    Note: this code should be run from the plugin to get a priority before the original code.

    To shorten your work, I have compiled this code into a small plugin and you can download it here.
    wpfts-addon-storefront-1.0.0.zip

    Was this helpful for you?