Ignoring links from the_excerpt

To strip the <a>...</a> tags from the output from the_excerpt(), you could use the the_excerpt filter and a regular expression search-and-replace.

Updated: In response to a comment, I’ve updated the code—the regex specifically—to strip out <a>...</a> and <a><span>...</a></span> constructions.

Updated again: In response to another comment, I’ve updated the code to use the get_the_excerpt filter, as that should catch both explicitly-set excerpts and excerpts generated from the post content.

add_filter( 'get_the_excerpt', 'wpse403907_strip_a_tags' );
/**
 * Strips <a> tags from the excerpt.
 *
 * @param  string $excerpt The excerpt.
 * @return string          The filtered excerpt.
 */
function wpse403907_strip_a_tags( $excerpt ) {
    $regex   = array(
        '|<a[^>]*>[^<]+</a>|',
        '|<a[^>]*><span[^>]*>[^<]+</span></a>|'
    );
    $excerpt = preg_replace( $regex, '', $excerpt );
    return $excerpt;
}

Add the above code to your active theme’s functions.php file.

The regular expression

If you’re unclear on what the regular expression (the 1st preg_replace() parameter, |<a[^>]*>[^<]+</a>|) does, here’s a quick breakdown:

  • | is the delimiter (the standard is to use / but since there’s a / in the string I’m searching for, I’ve used a different character)
  • <a[^>]* searches for any string starting with <a and ending with >, eg, <a href="example.com" title="This is some text">.
  • [^<]+ searches for one or more characters that isn’t <, ie, the opening of </a>.
  • </a> matches the end of the a tag.
  • | is the end delimiter.

Note that if you’ve got any excerpts with other HTML tags inside the <a>...</a>, eg, ...<a href="example.com">this link has <strong>bold text</strong> in it</a>..., this regex will not work correctly.

References