How can I create a “Read More” link using the_excerpt() on a static front page?

I’m working on a website that uses a static front page. It also shows the single most recent blog post. This was achieved by creating a page and using a custom page template.

Sometimes, the blog post is too long, so I want to use the_excerpt to automatically shorten it without the need for a more tag.

Read More

So far so good. But, the_excerpt doesn’t actually create a “read more” link. This is a pretty common problem, so I added:

<?php
function new_excerpt_more($more) {
    global $post;
    return '... <a href="'. get_permalink($post->ID) . '">continue reading</a>.';
}
add_filter('excerpt_more', 'new_excerpt_more');
?>

to my functions.php file.

I’ve actually used this code without issue on another site, but for whatever reason, it’s not working in this case. My initial guess was that it was because it’s being called on a static page.

The website is http://stuandjessproductions.com. The theme is Central by QODE, and I’m using a custom child theme.

EDIT

Adding code from the template page, as per request. This isn’t the whole page, but rather just the relevant bit for the news post:

<?php $query = "showposts=1&orderby='date'"; query_posts($query);?>
<?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
    <a href="<?php the_permalink();?>"><?php the_post_thumbnail('home'); ?></a>
    <div class="overlay">Latest News</div>
    <h4><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h4>
    <?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>

Related posts

1 comment

  1. On the post edit page, if you fill Excerpt box with any text, the_excerpt() function doesn’t add read more link or ... at the end of the short description at frontend. Read more link is only included if Excerpt is set empty. This is not a bug, it’s a default behavior.

    The solution is to avoid the excerpt_more filter to return read more link, and use the the_excerpt hook to add read more link.

    // excerpt_more should be set the empty.
    add_filter( 'excerpt_more', '__return_empty_string', 21 );
    
    function wpse_134143_excerpt_more_link( $excerpt ) {
        $excerpt .= sprintf( 
                '... <a href="%s">%s</a>.',
                esc_url( get_permalink() ),
                __( 'continue reading' )
        );
        return $excerpt;
    }
    add_filter( 'the_excerpt', 'wpse_134143_excerpt_more_link', 21 );
    

    Above code could go to your themes functions.php file.

Comments are closed.