最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

the content - How to get excerpt correctly formatted

programmeradmin2浏览0评论

I'm using a theme that use the_excerpt() function to show the excerpt of the post.

All posts have not set custom excerpt, so the_excerpt() returns a piece of post content.

In some post first paragraph contains a <br>, for example:

<p>My new question is:<br>why words are not separated by a white space?</p>

The text rendered is:

My new question is:why words are not separated by a white space?

Following this post I implemented this solution:

function my_excerpt($text = '', $raw_excerpt = '') {
    add_filter('the_content', 'my_content', 6);

    // get through origin filter
    $text = wp_trim_excerpt($text);

    remove_filter('the_content', 'my_content', 6);
    return $text;
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt');
add_filter( 'get_the_excerpt', 'my_excerpt');

function my_content($text)
{
    return str_replace( '<br>', ' ', $text );
}

And this works, but I have two questions:

  1. Why the_excerpt() doesn't substitute <br> with space?
  2. Is there a better way to achieve this result?

Also, I'm quite new to wordpress development, any suggestions to improve my code are welcome.

Update: I found that is reported this issue, but unfortunately is still open.

I'm using a theme that use the_excerpt() function to show the excerpt of the post.

All posts have not set custom excerpt, so the_excerpt() returns a piece of post content.

In some post first paragraph contains a <br>, for example:

<p>My new question is:<br>why words are not separated by a white space?</p>

The text rendered is:

My new question is:why words are not separated by a white space?

Following this post I implemented this solution:

function my_excerpt($text = '', $raw_excerpt = '') {
    add_filter('the_content', 'my_content', 6);

    // get through origin filter
    $text = wp_trim_excerpt($text);

    remove_filter('the_content', 'my_content', 6);
    return $text;
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt');
add_filter( 'get_the_excerpt', 'my_excerpt');

function my_content($text)
{
    return str_replace( '<br>', ' ', $text );
}

And this works, but I have two questions:

  1. Why the_excerpt() doesn't substitute <br> with space?
  2. Is there a better way to achieve this result?

Also, I'm quite new to wordpress development, any suggestions to improve my code are welcome.

Update: I found that is reported this issue, but unfortunately is still open.

Share Improve this question edited Oct 30, 2020 at 18:15 Lety asked Oct 30, 2020 at 14:41 LetyLety 1317 bronze badges 4
  • 1 Why would it substitute with a space? That would break things in some situations – Tom J Nowell Commented Oct 30, 2020 at 15:53
  • 1 @TomJNowell In the page that show article with a little piece of text (like categories or post per years) sentence are showed in a messed way, like the example above. So I want that when the excerpt is automatically generated from content, it is rendered in a correct way. Did you think that my code can break things? could you explain when and why? Thanks – Lety Commented Oct 30, 2020 at 16:00
  • 1 Whitespace would make sense to me too. I think it'd be better to change wp_strip_all_tags to add the whitespace though and leave the_excerpt alone. There's probably other cases e.g. if there was no whitespace in the markup when one paragraph ends and another starts e.g. 'abc</p><p>def', and other versions of br e.g. <br/>, <br /> - or with any attribute too really - but simple <br> is probably the most common.. – Rup Commented Oct 31, 2020 at 11:29
  • @Rup thanks. I see where is the problem. wp_strip_all_tags use php function strip_tags that remove alla html tag and generate messy excerpt both in case of br and in case of </p><p>. Probably classic editor put white space before br and at the end of p tag. I've used the_content filter that is applied just before text manipulation in wp_trim_excerpt. I should improve my str_replace to keep other br as you point out. Could you help me? Something like: <br>|<br/>|<br />? I'm not very good with regular expressions. – Lety Commented Oct 31, 2020 at 14:54
Add a comment  | 

1 Answer 1

Reset to default 2

wp_trim_excerpt() calls wp_trim_words() which applies wp_strip_all_tags() to the excerpt. The <br> tag is removed and not replaced with anything.

Not sure if this is feasible, but having a space after a colon is grammatically correct, and adding it to the original content would not affect the HTML, e.g.:

<p>My new question is: <br>why words are not separated by a white space?</p>

发布评论

评论列表(0)

  1. 暂无评论