Reputation: 422
Within Wordpress, is it possible to read the content of a post and look for keywords, then display sidebar content accordingly? Example:
If post content contains the word 'cheese' then don't display sidebar advert, otherwise do.
For extra information, I have >500 posts so would not want to add a tag or custom field to every post.
I'd include examples of code but I'm really not sure whether to start with a regex in functions.php and if so, what do I then look for in the sidebar code?
Thanks in advance.
UPDATE 1 - Stripos seems faster than regex for this purpose Stripos on php.net so I used this.
UPDATE 2 - My current setup... In index.php (or page.php etc depending on theme):
<?php
if( has_keyword() ) {
get_sidebar( 'special' );
} else {
get_sidebar( 'normal' );
}
?>
and in functions.php
function has_keyword ()
{
global $post;
$mywords = array('word1', 'word2', 'word3');
foreach($mywords as $word){
// return false if post content does not contain keyword
if( ( stripos( $post->post_content, $word ) === false ) ) {
return false;
};
};
// return true if it does
return true;
}; //end function
I need to get the foreach function working, there is something wrong in there. I tried to use 'break' on successfully finding a word but I need to return 'false' as well, that's why I added the if condition. Not sure how to do this.
Upvotes: 0
Views: 692
Reputation: 6838
You could use PHP's stripos
. Define a custom conditional tag in functions.php
:
function has_keyword( $keyword )
{
// only check on single post pages
if( ! is_singular() )
return false;
global $post;
// return false if post content does not contain keyword
if( ( stripos( $post->post_content, $keyword ) === false ) )
return false;
// return true if it does
return true;
}
Then, in your template file:
if( has_keyword( 'my_keyword' ) )
get_sidebar( 'normal' );
else
get_sidebar( 'special' );
update
To check for multiple keywords (see comments):
function has_keyword()
{
if( ! is_singular() )
return false;
global $post;
$keywords = array( 'ham', 'cheese' );
foreach( $keywords as $keyword )
if( stripos( $post->post_content, $keyword ) )
return true;
return false;
}
Upvotes: 5
Reputation: 64476
you can also use preg_match
to find exact keyword match in the string like
function check_keyword($keyword){
global $post;
if(!is_single() ){
return false;
}else{
$result = preg_match('/\b('.$keyword.')\b/', $post->post_content);
if($result){
return true;
}else{
return false;
}
}
}
To get side_bar
Call check_keyword()
if (check_keyword('cheese')) {
get_sidebar('cheese');
} else {
get_sidebar('no-ads');
}
See for reference preg_match() Hope it makes sense
Upvotes: 1
Reputation: 1086
If you want to verify against a list of words, you could use this function below, which will return false if any of the words are found in your $content else it will return true. So as to say go ahead and display them ads.
function displayAds($content){
$words = array('cheese', 'ham', 'xxx');
foreach($words as $word){
if(preg_match('/\s'.$word.'\s/i', $content)){
return FALSE;
};
};
return TRUE;
};
then in your index.php you can do as your outloud thinking in your Update. Naturally changing function names to reflect your choice of naming.
Upvotes: 1