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 );
}
}
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.
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.
the_excerpt
filterget_the_excerpt
filterpreg_replace()
<p><?php the_excerpt(); ?></p>
– Jdubbs Commented Mar 19, 2022 at 22:00this is a <a>link</a>
becomesthis is a
? Or you mean it becomesthis 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:12this is a <a>link</a>
would becomethis is a
– Jdubbs Commented Mar 19, 2022 at 22:21