0

I've just imported a blog into Wordpress and all of the content begins with:

<a href="itunes.com">Listen on iTunes</a>

followed by the paragraph content, so all excerpts appear with "Listen on iTunes content excerpt..."

I've tried a few custom functions found here, but none seem to do the trick. What's the best way to remove the Listen on iTunes text without having to move the iTunes link?

For example, I tried this... with no luck...

    function custom_excerpt() {
    $text = get_the_excerpt(); //Assigns the excerpt to $text
    $text = str_replace("Listen","", $text); // replacing the word with empty string
    $text = str_replace("on","", $text);
    $text = str_replace("iTunes","", $text);
    return $text;
}
add_filter('the_excerpt', 'custom_excerpt');

1 Answer 1

1

The way you are using the filter is wrong. Your function should accept the excerpt as an argument:

function custom_excerpt( $excerpt ) {

Then you don't need to do the get_the_excerpt() call, as you already have the value you want to edit. Furthermore, if you are replacing each word, one by one, you could hurt other parts of the excerpt, so I'd advise you to remove the whole sentence in a single call of str_replace().

3
  • Thanks for the hand. I came up with the following adjustments: function custom_excerpt($excerpt) { $text = str_replace("Listen on iTunes","", $text); // replacing the phrase with empty string return $text; } add_filter('excerpt', 'custom_excerpt'); however, it's still not working. any help as to where I'm going wrong is appreciated!
    – Dave
    Aug 30, 2021 at 19:17
  • @Dave, you are calling str_replace on an empty string, instead of the $excerpt argument, that comes from the filter. Also, consider setting a priority to add_filter, just to be sure you are doing the replace after any other functions.
    – vlood
    Sep 1, 2021 at 6:56
  • function custom_excerpt($excerpt) { $excerpt = str_replace("Listen on iTunes","", $excerpt); return $excerpt; } add_filter('excerpt', 'custom_excerpt', 999); Thank you @vlood, Took another stab at it with our sys admin and added the code to our functions.php, but the theme is still showing the stripped HTML from the post, specifically: '<a href="">Listen on iTunes</a> ' and we're hoping a theme override will save us from having to strip the content to generate custom excerpts. The link is from our migration and we have several hundred posts with the link.
    – Dave
    Sep 8, 2021 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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