admin管理员组文章数量:1122832
I'm trying to search through the content of the current post and look for any strings in the content that match the title of any 'term' custom post types.
The idea is that any terms we have that are in the post description are right there for a user to view if they need clarification. This script works perfectly when I put in any string for the variable $content_search, but stops working properly when I set
$content_search = get_the_content();
It spits out the matching terms, but also a few seemingly random ones as well.
I've been staring at this block of code for quite some time and I just can't figure out what's wrong. Any help would be awesome!
<?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();
?>
I'm trying to search through the content of the current post and look for any strings in the content that match the title of any 'term' custom post types.
The idea is that any terms we have that are in the post description are right there for a user to view if they need clarification. This script works perfectly when I put in any string for the variable $content_search, but stops working properly when I set
$content_search = get_the_content();
It spits out the matching terms, but also a few seemingly random ones as well.
I've been staring at this block of code for quite some time and I just can't figure out what's wrong. Any help would be awesome!
<?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();
?>
Share
Improve this question
asked Sep 6, 2014 at 0:14
user57391user57391
677 bronze badges
1 Answer
Reset to default 0The problem is that you are matching the title string in the entire content as whole and not word by word. e.g. if your title is 'blog' and content contains the word 'blogging', it will still match and pull 'blog' from 'blogging'. So, you can compare them word by word by exploding the content as follows:
<?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();
?>
本文标签: searchDisplay post titles that match string in post content
版权声明:本文标题:search - Display post titles that match string in post content 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295593a1929600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论