Cutting text and/or replacing the standard the_excerpt() function
I noticed that the WordPress function the_excerpt() is cumbersome. It takes a lot of time and resources to execute it. This is because it calls get_the_excerpt(), the_content() and applies various hooks to them, including the_excerpt() itself. As a result, there are quite a few operations - this is not always necessary. For example, I treat quotes simply - a short description of the article, cutting out a small piece of it.
I put up with this drawback until I needed to trim the text to a certain number of characters. That's when I decided to write a text-cropping function.
The result of replacing the_excerpt() pleased me: the page generation decreased on average from 0.850 seconds to 0.550 seconds, with 9 calls to the_excerpt() (this is on a computer, on the server, it's usually less). 9 calls - this is the number of posts displayed in the category, to each of which the_excerpt() was applied.
Below is the function that can replace the standard WordPress function the_excerpt().
How the function works
-
Cuts to a certain number of characters. Specified in the maxchar parameter.
-
Understands the <!--more--> tag in the record. If it is present, the desired number of characters to be displayed is ignored, and everything above <!--more--> is output with the HTML markup preserved.
-
You can specify whether to keep line breaks or write the entire text in one line. By default, line breaks are preserved, if you need a "continuous text", set the autop = 0 parameter.
-
You can specify which HTML tags should not be cut out. For example, if we want to leave the <strong> and <em> tags, then specify them like this: save_tags=<strong><em>
- The function can also be used to crop any text passed to it. To do this, specify the text in the text parameter.
In all cases, the cropping calculates the number of characters and then removes the last characters until a space. This is necessary so that in the end there is always a complete word left, not a piece of unfinished one (I think it's ugly).
Using
Insert the code mentioned above into the functions.php file of your theme. And where you need to output the cropped text, call the function like this:
<?php echo kama_excerpt( [ 'maxchar'=>100, 'text'=>'blabla' ] ); ?>
To replace the standard the_excerpt(), just replace the_excerpt() with kama_excerpt(). All this should be inside the WordPress loop.
IMPORTANT: The text parameter is not needed when replacing the_excerpt()!
Example of using the function to crop any text:
$str = "The function [foo]some text[/foo] [foo]some text[/foo] of text cropping for Worpress, can be applied [foo url='bar'] and on other engines."; echo kama_excerpt([ 'text'=>$str, 'maxchar'=>70 ]);
We will get:
<p>The function of text cropping for Worpress,<br /> can be applied and on other ...</p>
When cropping the text passed to the function, only that part of the function that is necessary (no extra operations) is triggered.
Very simple example of text cropping
If you don't want to use the function and just need to crop the text without saving html tags and so on, you can use this short string inside the WordPress loop:
<?php $maxchar = 152; $text = strip_tags( get_the_content() ); echo mb_substr( $text, 0, $maxchar ); ?>
152 - this is the number of characters left.
Or
Use the wp_trim_words() function.