Compare title and category name in wordpress

I had come through a situation to compare the category name and post title for a wordpress post. The following functions and code helped me to do this.

$category = get_the_category();
$cat = $category[0]->cat_name;
$post_no = get_post($post);
$title = $post_no->post_title;
$match=strcmp($cat,$title);
if($match==0){
echo “match”;
}
else
{
echo “no match”;
}

The problem here is $cat = $category[0]->cat_name; returns the first category name, so we are comparing only the first category with the title. If we want to compare the second category we have to use $cat = $category[1]->cat_name;

I hope someone will write a better solution so that we can compare the array, category with title. I would appreciate if someone can post that…

in_array came to my mind, but it is case sensitive…

Leave a Reply