我正在尝试搜索当前帖子的内容,并在内容中查找与任何“术语”自定义帖子类型标题匹配的字符串。
我们的想法是,如果用户需要澄清,帖子描述中的任何术语都可以供用户查看。当我为变量$content\\u search输入任何字符串时,该脚本都能正常工作,但当我设置
$content_search = get_the_content();
它会吐出匹配项,但也会吐出一些看似随机的项。
我已经盯着这段代码看了很长一段时间了,我就是不知道出了什么问题。任何帮助都会很棒!
<?php
// search for matching glossary terms that are in the description
$content_search = get_the_content();
$content_search = strtolower($content_search);
$args_terms = array(
\'post_type\' => \'term\',
\'orderby\' => \'title\',
\'posts_per_page\' => -1
);
$glossary_terms = new WP_Query($args_terms);
echo \'<h3>Terms Used in this Lesson:</h3>\';
while ($glossary_terms->have_posts()){
$glossary_terms->the_post();
$tt_title = strtolower(get_the_title());
if (strpos($content_search, $tt_title) !== false){
$tt_title = ucwords($tt_title);
$tt_content = strip_tags(get_the_content());
echo do_shortcode(\'[tooltip tooltip_title="\' . $tt_content . \'"]\' . $tt_title . \'[/tooltip]\');
echo \' | \';
}
}
wp_reset_postdata();
?>
SO网友:Domain
问题是,您在整个内容中匹配的是标题字符串,而不是逐字匹配。e、 g.如果你的标题是“blog”,而内容包含“blogging”一词,它仍然会匹配并从“blogging”中提取“blog”。因此,您可以按如下方式分解内容,逐字比较它们:
<?php
// search for matching glossary terms that are in the description
$content_search = get_the_content();
$content_search = strtolower($content_search);
$args_terms = array(
\'post_type\' => \'term\',
\'orderby\' => \'title\',
\'posts_per_page\' => -1
);
$glossary_terms = new WP_Query($args_terms);
echo \'<h3>Terms Used in this Lesson:</h3>\';
while ($glossary_terms->have_posts()){
$glossary_terms->the_post();
$tt_title = strtolower(get_the_title());
$content = explode(" ", $content_search);
foreach($content as $term){
if ($term === $tt_title){
$tt_title = ucwords($tt_title);
$tt_content = strip_tags(get_the_content());
echo do_shortcode(\'[tooltip tooltip_title="\' . $tt_content . \'"]\' . $tt_title . \'[/tooltip]\');
echo \' | \';
}
}
}
wp_reset_postdata();
?>