How to make the excerpt_more filter apply to the actual post excerpt?

In the get_blog_excerpt() function below, the excerpt_more filter works perfectly when the post excerpt is not present, however, when the post has an excerpt, I’m not getting the “Read More” link.

I understand that the_excerpt first checks to see if a post excerpt is present, which is fine, but I want the read more link applied to it too.

Read More

What do I need to change in order to make the excerpt_more apply in all cases?

function get_blog_excerpt(){
    add_filter('excerpt_length', 'ce4_excerpt_length');
    add_filter('excerpt_more', 'ce4_excerpt_more');
    return the_excerpt();
}

function ce4_excerpt_length($length) {
    return 150;
}

function ce4_excerpt_more($more) {
    global $post;
    return '...<a href="'. get_permalink($post->ID) . '">Read More</a>';
}


function get_blog_links(){
    global $post;
    setup_postdata($post);
    $myposts = get_posts($args);echo '<div id="menuFooterRecent" class="blog">'; 
    echo '<ul>'; 
    foreach($myposts as $idx=>$post){ ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php 
    echo get_blog_excerpt();
    echo '<div style="clear:both">&nbsp;</div>';?></li>
    <?php } echo '</ul></div>';
}

The code above is inside functions.php

The code below is inside archive.php

<?php 
if(is_category()){
    if (get_query_var('cat') == get_category_by_slug('blog')->term_id){
        get_blog_links();
    }
    else 
    {
    get_category_links();
    }
} ?>    

Related posts

Leave a Reply

1 comment

  1. I’m going to assume that you’re calling get_blog_excerpt() in your template somewhere?

    If so, what happens if you simply call the_excerpt(), and then pull the two add_filter() calls out of the container function? i.e. functions.php would just look like:

    function ce4_excerpt_length($length) {
        return 150;
    }
    add_filter('excerpt_length', 'ce4_excerpt_length');
    
    function ce4_excerpt_more($more) {
        global $post;
        return '...<a href="'. get_permalink($post->ID) . '">Read More</a>';
    }
    add_filter('excerpt_more', 'ce4_excerpt_more');
    

    And in your template, you would just call the_excerpt().

    If that works, then I suspect the issue is that your filters aren’t getting applied – probably due to being wrapped in the container function.