0

I have a section in my header that shows 2 excerpts from the latest posts which are potentially from a regular blog post or custom post. The custom posts are utilising Advanced Custom Fields to show a video, a description and some other things in the single.php. Because of this I needed to change some things about the excerpt filters in my functions.php file. This all works perfectly but the problem is that the description can be less than 15 words which seems to be my minimum for the excerpt_more filter which means that I don't get a '...Read More' link or anything else to allow a user to click through into the post. Here is my code for this section:

function new_excerpt_length($length) {
    return 15;
} 
add_filter('excerpt_length', 'new_excerpt_length');

function new_excerpt_more($more) {
    global $post;
    return '<a class="moretag" href="'. get_permalink($post->ID) . '"><br>...Read More</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

function custom_field_excerpt() {
    global $post;
    $text = get_field('description'); 
    if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]&gt;', ']]&gt;', $text);
        $excerpt_length = 15;
        $excerpt_more = apply_filters('excerpt_more', ' ');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more);
    }
    return apply_filters('the_excerpt', $text);
}

Is there something I could add to this or perhaps change the new_excerpt_more function in some way to always add a 'Read More' instead of just if it meets the 15 word minimum?

1

You can count the number of words in your text to check if it's greater than 15 using the str_word_count php function, and if not return your read more link also

So i would modify your custom field excerpt thus:

function custom_field_excerpt() {
    global $post;
    $text = get_field('description');
    if ('' != $text) {
        $text = strip_shortcodes($text);
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]&gt;', ']]&gt;', $text);
        $excerpt_length = 15;
        $excerpt_more = apply_filters('excerpt_more', ' ');
        $text = wp_trim_words($text, $excerpt_length, $excerpt_more);
    }

    if (str_word_count($text) > 15)
        return apply_filters('the_excerpt', $text);

    return apply_filters('the_excerpt', $text) . "<a class='moretag' href='get_permalink($post->ID)'><br>...Read More</a>"
}
  • Also remember to upvote the answer too :) – Tunji Jan 8 '17 at 16:49
  • I can't yet, not enough points haha! One thing though, it appears in a separate <p> tag like this -> <p>excerpt</p> <p>...Read More</p> <p>another excerpt + ...Read More</p> do you know why this would be? – JonHerbert Jan 8 '17 at 16:56
  • The second one in that example is working because it is over 15 words btw – JonHerbert Jan 8 '17 at 16:56

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.