9

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.

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; ?>
2
  • what is the code of the page template?
    – Michael
    Feb 11 '14 at 9:50
  • I've added some of the code from the template page (the relevant bit). If you want to see the whole thing, I can post it... but it's kinda long.
    – convoke
    Feb 11 '14 at 13:55
16

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.

8
  • I haven't added anything to the Excerpt text field on the post, nor do I intend to. Like I said, the idea is to make things as low maintenance as possible for the client. The current code does truncate the post to 55 words, or whatever the default is, and adds the ellipsis. The problem is that my normal method for turning that ellipsis into a "Read More" link doesn't seem to be working.
    – convoke
    Feb 11 '14 at 13:53
  • So, I tried your code, and it seems to work. Only problem is, I get a double ellipsis. It's live on stuandjessproductions.com right now, if you want to see. Do you have any ideas?
    – convoke
    Feb 11 '14 at 16:28
  • I should add that I removed my original code from functions.php.
    – convoke
    Feb 11 '14 at 16:30
  • I marked this question as an answer, but the solution is till injecting two "read more" tags. I just removed the ellipsis from the second tag, which works for me, but it's not really perfect.
    – convoke
    Feb 13 '14 at 19:49
  • 1
    @NavnishBhardwaj - 21 is the filter priority here. There you can you any positive number more than 0.
    – Shazzad
    Apr 11 '18 at 13:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.