2017-07-17 68 views
0

我有这个在我的WordPress模板:如何从Wordpress获取内容时修剪特定单词?

$my_content = get_the_content(); 
$trimmed_content = wp_trim_words($my_content, 12, '...'); 
echo $trimmed_content; 

此修剪,我获取的内容,但我想摆脱的一些话我不想表现出明确。因此,我们假设我不想在内容中显示“来”“世界”“天气”

我该如何做到这一点?

我对此有一个相当奇怪的要求。我有一些词作为标签使用。所以我们可以说是这样的:(标签: hereComesArandomWord)

我想要做的,就是要摆脱的(标签:)(因此也是最后parenthese)。这可能吗?

+0

请注明更多。每个**来**字你不想**任何特定的地方来**字? –

+0

你是什么意思?不,我只是想摆脱一些话。非常特别。我会在我的问题中提供更多示例。 – Siyah

回答

2

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; 

文档:http://php.net/manual/en/function.str-replace.php

+0

是的,太棒了。非常感谢你! – Siyah

+0

你对另一个人有一个正确的答案,顺便说一句?我的意思是,删除这个:(tag:)? – Siyah

+0

假设它是你主要内容的一部分(put标签:在数组中),同样的方法也适用。如果分开处理,那么你的问题不包含足够的信息来提供答案。纯粹作为猜测,虽然看看WordPress文档中的'the_tags()'。第一个参数可让您设置(或删除)之前的文本 –

0

使用str_replace隐藏(删除)字符串。

作为一个例子:

$trimmed_content = str_replace(array("coming", "world", "weather"), "", $trimmed_content); 
echo $trimmed_content; 
+0

是的,但这并不能确认我自己的参数和设置。我想修剪它,并显示前12个单词...加上“...”,最后你会看到。你能补充吗? – Siyah

0

HTML

Lorem存有悲坐阿梅德,consectetur adipiscing ELIT,sed的做eiusmod tempor incididunt UT labore等dolore蚤aliqua。请将您的评论发送给我们,我们会尽快为您解答。 Duis aute irure dolor in renhenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Excepteur烧结靶occaecat cupidatat非proident,必须遵守的过失魁正式开通了deserunt mollit阿尼姆ID EST laborum ...

修剪特定的词

<p><?php echo wp_trim_words(get_the_content(), 70, false); ?></p>