admin管理员组

文章数量:1122846

I just noticed a weird behavior on a theme I'm working on. If I have a post with just one link or shortcode, the_excerpt will print that URL or shortcode. But if I have some text in the post, below or above the same link or shortcode, the_excerpt returns only the text.

Why is this happening? And is there a simple way to remove the links and shortcodes if there is no other content in a post?

The only filters I used on the_excerpt are the excerpt_more and excerpt_length. Also, in my loop I'm using the_excerpt();.

-- Later edit --

After exchanging a few comments below it became obvious that I should also include a few examples in this questions to clarify two thinks: 1) the default behavior of the_excerpt and 2) the case and solution I'm looking for.

Default behavior of the_excerpt:

  1. If there is content below or above a shortcode, only the content (text) will be shown by the_excerpt.

  2. If there is no content below or above a shortcode that contains an URL, the URL will be shown by the_excerpt. The same happens if instead of a shortcode thre's an embeddable URL (like a youtube link).

Example: If I have in a post only this shortcode [audio mp3=""][/audio], the excerpt will print only the URL -> .

What I would want to do is this: if the_excerpt displays only an URL, then display 'no content'.

I just noticed a weird behavior on a theme I'm working on. If I have a post with just one link or shortcode, the_excerpt will print that URL or shortcode. But if I have some text in the post, below or above the same link or shortcode, the_excerpt returns only the text.

Why is this happening? And is there a simple way to remove the links and shortcodes if there is no other content in a post?

The only filters I used on the_excerpt are the excerpt_more and excerpt_length. Also, in my loop I'm using the_excerpt();.

-- Later edit --

After exchanging a few comments below it became obvious that I should also include a few examples in this questions to clarify two thinks: 1) the default behavior of the_excerpt and 2) the case and solution I'm looking for.

Default behavior of the_excerpt:

  1. If there is content below or above a shortcode, only the content (text) will be shown by the_excerpt.

  2. If there is no content below or above a shortcode that contains an URL, the URL will be shown by the_excerpt. The same happens if instead of a shortcode thre's an embeddable URL (like a youtube link).

Example: If I have in a post only this shortcode [audio mp3="http://path.to.file"][/audio], the excerpt will print only the URL -> http://path.to.file.

What I would want to do is this: if the_excerpt displays only an URL, then display 'no content'.

Share Improve this question edited Dec 19, 2014 at 14:39 George Grigorita asked Dec 18, 2014 at 0:15 George GrigoritaGeorge Grigorita 6192 gold badges9 silver badges21 bronze badges 9
  • The codex says "An auto-generated excerpt will also have all shortcodes and tags removed". Can you remove your filters and check if the problem persists?. – cybmeta Commented Dec 18, 2014 at 9:24
  • Yes, that is correct, but the post will still display the URL of the shortcode (if it's an youtube, mp3 or anything embeddable). If I put a filter to remove the URL from the_excerpt, then the post will show the shortcode. Maybe if I try a regex to remove the URLs and then also a strip_shortcodes... – George Grigorita Commented Dec 18, 2014 at 12:35
  • If you have a shortcode with a URL inside the shortcode, auto-generated excerpt will print the URL, not the shortcode, and that is the normal and correct behaviour. If you has active filters to alter that behaviour, please post the code of those filters. – cybmeta Commented Dec 18, 2014 at 14:06
  • Ok, so that's the normal behavior of the_excerpt: to show the URL. Is there any way I could hide the URLs? And no, I don't have any active filters. – George Grigorita Commented Dec 18, 2014 at 17:58
  • I thought you had active filters because you said that. I'm sorry but I was wrong if you have something like [shortcode]http://someurl.com[/shortcode], the auto generated excerpt should print empty string, that is nothing. If you have "Some text [shortcode]someurl.com[/shortcode]", then the auto generated excerpt should print only "Some text". You should post the actual code you are using, include any excerpt filter, content filter and the shortcode code. If you have only a link, the autogenerated excerpt should print the anchor text of the link. – cybmeta Commented Dec 18, 2014 at 18:43
 |  Show 4 more comments

1 Answer 1

Reset to default 0

when outputting you can use strip_shortcodes( $content ) which will remove all shortcode tags from the given content.

本文标签: theme developmentFilter URL and shortcodes from theexcerpt