0

I am looking for a way to ignore any <a> links in the content WP displays in the the_excerpt - is there a way I can achieve this?

The excerpt is not actually used on the WP page itself, so the main content area of the page is displayed - but I want to make sure any <a> links are not included. Is that possible?

So, what I am looking for is that if the excerpt was this:

Hello, this is the content currently included in the_excerpt, with <a><span>a link</span></a> here and some more content here....

Then the output of the_excerpt would remove the full <a> element and so the excerpt would become:

Hello, this is the content currently included in the_excerpt, with here and some more content here....

To display the content, I am using:

<p><?php the_excerpt(); ?></p>

Update - The excerpt field is actually empty on the pages that i am referencing in the loop, so it defaults to pull the content from the main body area of the page. I guess this makes a difference to the code that might need to be applied here?

The custom theme code to pull page content if the_excerpt is empty is:

if ( has_excerpt() ) {
        $the_excerpt = get_the_excerpt();
        $the_excerpt = preg_replace( '/\[[^\]]+\]/', '', $the_excerpt );  // strip shortcodes, keep shortcode content
        return wp_trim_words( $the_excerpt, $limit );
    } else {
        $the_content = get_the_content();
        $the_content = preg_replace( '/\[[^\]]+\]/', '', $the_content );  // strip shortcodes, keep shortcode content
        return wp_trim_words( $the_content, $limit );
    }
}
8
  • can you be more specific what you meant by ignore? You want to strip the hyperlinks out? Or you want to make them plaintext? Or you want excerpts with links to not be shown? Or you want the hyperlinks to be present but unclickable? if your excerpt is not showing on the page and the main content is being shown instead, then I'm unsure what you're trying to achieve. I also see there is no code to work with in your question
    – Tom J Nowell
    Mar 19, 2022 at 21:55
  • I want to remove them totally so they dont appear in the text that appears where the_excerpt is processed. RE:code - Im just using - <p><?php the_excerpt(); ?></p>
    – Jdubbs
    Mar 19, 2022 at 22:00
  • so you mean this is a <a>link</a> becomes this is a? Or you mean it becomes this is a link? Remove them totally is unclear, and you provide no examples of before/after
    – Tom J Nowell
    Mar 19, 2022 at 22:12
  • this is a <a>link</a> would become this is a
    – Jdubbs
    Mar 19, 2022 at 22:21
  • I have a link at the end of the main content area that I would to ignore for short page content - so it doesnt appear in the excerpt
    – Jdubbs
    Mar 19, 2022 at 22:21

1 Answer 1

0

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

2
  • Thanks - this doesnt seem to have changed anything when i look at what the_excerpt displays - the text within the <a> links remains in view. Looks like the <a> link has a <span> wrapping the text inside the <a> - can this be adapted to work for links like this?
    – Jdubbs
    Mar 20, 2022 at 22:10
  • Thanks for the update - the link text still appears for some reason. I am wondering if its because the actual excerpt field is empty - so WP is pulling the content from the main body content area of the page - would this mean your code would not work?
    – Jdubbs
    Mar 21, 2022 at 9:32

Your Answer

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

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