从 Wordpress 获取内容时如何 trim 特定单词?
How to trim specific words when getting content from Wordpress?
我的 Wordpress 模板中有这个:
$my_content = get_the_content();
$trimmed_content = wp_trim_words( $my_content, 12, '...' );
echo $trimmed_content;
这修剪了我正在获取的内容,但是我想去掉一些我不想显式显示的词。所以假设我不想显示 "coming" 或 "world" 或 "weather"在内容中。
我怎样才能做到这一点?
关于这个,我有一个相当奇怪的请求。我有一些词用作标签。所以让我们这样说:(tag: hereComesArandomWord)
我想做的是去掉 (tag: ) (最后一个括号也是如此)。这也可以吗?
使用str_replace
隐藏(删除)字符串。
举个例子:
$trimmed_content = str_replace(array("coming", "world", "weather"), "", $trimmed_content);
echo $trimmed_content;
str_replace()
将对字符串进行搜索和替换。
之前的回答多多少少都在钱上。您只需使用 wp_trim_words()
.
在 之前执行 str_replace()
示例:
$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;
HTML
事实是,痛苦本身很重要,学生的教育也会随之而来,但这次他们会像一些伟大的工作和痛苦一样倒下。就最小的细节而言,任何人都不应从事任何一种工作,除非他从中得到一些好处。不要在痛斥中生气在快感中痛斥他要从痛中发一毛希望没有滋生。除非他们被欲望蒙蔽了双眼,他们不出来,他们有错放弃了他们的职责,灵魂软化了,那就是劳动...
trim具体字词
<p><?php echo wp_trim_words(get_the_content(), 70, false); ?></p>
我的 Wordpress 模板中有这个:
$my_content = get_the_content();
$trimmed_content = wp_trim_words( $my_content, 12, '...' );
echo $trimmed_content;
这修剪了我正在获取的内容,但是我想去掉一些我不想显式显示的词。所以假设我不想显示 "coming" 或 "world" 或 "weather"在内容中。
我怎样才能做到这一点?
关于这个,我有一个相当奇怪的请求。我有一些词用作标签。所以让我们这样说:(tag: hereComesArandomWord)
我想做的是去掉 (tag: ) (最后一个括号也是如此)。这也可以吗?
使用str_replace
隐藏(删除)字符串。
举个例子:
$trimmed_content = str_replace(array("coming", "world", "weather"), "", $trimmed_content);
echo $trimmed_content;
str_replace()
将对字符串进行搜索和替换。
之前的回答多多少少都在钱上。您只需使用 wp_trim_words()
.
str_replace()
示例:
$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;
HTML
事实是,痛苦本身很重要,学生的教育也会随之而来,但这次他们会像一些伟大的工作和痛苦一样倒下。就最小的细节而言,任何人都不应从事任何一种工作,除非他从中得到一些好处。不要在痛斥中生气在快感中痛斥他要从痛中发一毛希望没有滋生。除非他们被欲望蒙蔽了双眼,他们不出来,他们有错放弃了他们的职责,灵魂软化了,那就是劳动...
trim具体字词
<p><?php echo wp_trim_words(get_the_content(), 70, false); ?></p>