5

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; ?>
  • 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
9

when you fill the Excerpt box with any text on post edit page, the_excerpt() function doesn't add any read more or ... at the end of the short description. Read more is only included if Excerpt is set empty, and this is not a bug, it's a feature.

Now the solution is to avoid the excerpt_more filter to return read more, and use the the_excerpt hook. But excerpt_more should be set the empty to make sure double read more is not displayed. So, the code you need -

function new_excerpt_more($more) {
    return '';
}
add_filter('excerpt_more', 'new_excerpt_more', 21 );

function the_excerpt_more_link( $excerpt ){
    $post = get_post();
    $excerpt .= '... <a href="'. get_permalink($post->ID) . '">continue reading</a>.';
    return $excerpt;
}
add_filter( 'the_excerpt', 'the_excerpt_more_link', 21 );

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

  • 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.