Reputation: 2897
I have this in my Wordpress template:
$my_content = get_the_content();
$trimmed_content = wp_trim_words( $my_content, 12, '...' );
echo $trimmed_content;
This trims the content I am fetching, but I want to get rid of some words I don't want to show explicitly. So let's say I don't want to show the word "coming" or "world" or "weather" in the content.
How can I achieve that?
And I have one rather odd request regarding this. I have some words which I use as tags. So let's say something like this: (tag: hereComesArandomWord)
What I want to do, is to get rid of (tag: ) (so also the last parenthese). Is this possible as well?
Upvotes: 0
Views: 2131
Reputation: 209
HTML
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum...
trim specific words
<p><?php echo wp_trim_words(get_the_content(), 70, false); ?></p>
Upvotes: 0
Reputation: 19154
use str_replace
to hide (remove) string.
As an example:
$trimmed_content = str_replace(array("coming", "world", "weather"), "", $trimmed_content);
echo $trimmed_content;
Upvotes: 0
Reputation: 19308
str_replace()
will operate a search and replace on a string.
The previous answer was more or less on the money. You'd simply execute str_replace()
before using wp_trim_words()
.
Example:
$my_content = get_the_content();
$filtered_content = str_replace( array( 'coming', 'world', 'weather' ), '', $my_content );
$trimmed_content = wp_trim_words( $filtered_content, 12, '...' );
echo $trimmed_content;
Documentation: http://php.net/manual/en/function.str-replace.php
Upvotes: 2