如果标题包含特定单词,则不显示帖子 WordPress



如果标题包含特定单词,有没有办法停止在循环中显示帖子?

我的代码是这样的:

<?php if(strpos(get_the_title(), 'Product1', 'Product2') === false) { } ?>

使用此代码,我的帖子根本没有标题。

您可以使用正则表达式检查是否包含单词。

if (!preg_match('/(word1|word2|word3)/', get_the_title()) {
  // title doesn't contain word1, word2, word3
}

要使大小写不区分,请在正则表达式中添加标志"i"。

'/(word1|word2|word3)/i'

最新更新