开发者

Compare Wordpress Tags with Post Content and output

开发者 https://www.devze.com 2023-03-25 09:09 出处:网络
OK Wordpress PHP gods, here is a good one.I want to compare the TAGS against the Post Content, if the TAG appears in the Post Content, then I want it to output.If it doesn\'t appear in the content, th

OK Wordpress PHP gods, here is a good one. I want to compare the TAGS against the Post Content, if the TAG appears in the Post Content, then I want it to output. If it doesn't appear in the content, then don't.

  1. I am presuming that I would need to output开发者_JAVA百科 the TAGS as an Array

  2. If I do the same for the Content, using (correct if incorrect) 'Explode' then each word would be single. Consider 'The Lazy Brown Fox', if I had a TAG 'Brown Fox'

So in short I am stumpped, and have no idea how to proceed. I am VERY sure that this post would interest many people.

Many thanks for your advice in advance

Stu


Rather than exploding the words in the post, why not use strpos()? This would take care of the multiple word tags.

$tags = get_the_tags($post->ID);
$stripped = strtolower(strip_tags($post->post_content));
$tags_to_output = array();
foreach($tags as $tag){
    if (strpos($stripped, strtolower($tag->name)) !== false){
        $tags_to_output[] = $tag;
    }
}

If you were really worried about it you could replace multiple whitespace characters in $stripped

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号