html - Ignoring <a> links from the_excerpt

admin2025-01-08  4

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 );
    }
}

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 );
    }
}
Share Improve this question edited Mar 21, 2022 at 10:14 Jdubbs asked Mar 19, 2022 at 21:16 JdubbsJdubbs 33 bronze badges 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 Commented 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 Commented 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 Commented Mar 19, 2022 at 22:12
  • this is a <a>link</a> would become this is a – Jdubbs Commented 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 Commented Mar 19, 2022 at 22:21
 |  Show 3 more comments

1 Answer 1

Reset to default 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

  • the_excerpt filter
  • get_the_excerpt filter
  • preg_replace()
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267649a1212.html

最新回复(0)