0

I am experiencing a really weird issue.

The built in function the_excerpt() as soon as the user inserts a custom excerpt form the back end return the excerpt which is produced dynamically by WP without a read more button. If the user does not insert a custom excerpt, the function returns the excerpt along with a read more button.

I want to have full control of this function. I used the code

if (!function_exists('sociality_excerpt_more')) {
 function sociality_excerpt_more($more) {
    return $more . '&nbsp<a class="read-more p-color" rel="bookmark"  title="'. get_the_title() .'" href="'. get_permalink($post->ID) . '">'. esc_html__('View more','g5plus-handmade') .'<i class="pe-7s-right-arrow"></i></a>'; 
}
add_filter('the_excerpt', 'sociality_excerpt_more');
}

but it does not have an effect. Using this snippet the function returns twice a read more button if the user does not insert a custom excerpt.

Any help appreciated :)

0

You can wrap your output in the has_excerpt function, this checks if the post has a custom excerpt.

if (!function_exists('sociality_excerpt_more')) {
    function sociality_excerpt_more($more) {
        if ( has_excerpt() ) {
            return $more . '&nbsp<a class="read-more p-color" rel="bookmark"  title="'. get_the_title() .'" href="'. get_permalink($post->ID) . '">'. esc_html__('View more','g5plus-handmade') .'<i class="pe-7s-right-arrow"></i></a>';
        } else {
            return $more;
        }
    }
    add_filter('the_excerpt', 'sociality_excerpt_more');
}
0

the_excerpt function should return just the excerpt without additional decoration, unless the user manually choose to add it to the manual excerpt. This is the expected behaviour of excerpts in wordpress and you should not try to override it as there are many places in which excerpts are (wrongly) used by calling the the_excerpt function.

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.