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…

One Response to “Compare title and category name in wordpress”

  1. admin0 says:

    I posted this in wordpress forum and the experts there helped me to do it in a better way. Here is the code:

    < ?php
    $cats = wp_get_post_categories($post->ID);

    if ($cats) {

    foreach ($cats as $cat) {

    $category = get_category($cat);

    if ( strcmp(strtoupper($post->post_title), strtoupper($category->name)) ) {

    echo ‘Match’;

    } } }
    ?>

© 2010 Some PHP | Wordpress Tips Suffusion WordPress theme by Sayontan Sinha