WordPress.org

Support

Support » Themes and Templates » [Resolved] Compare title and category of a wordpress post

[Resolved] Compare title and category of a wordpress post

  • Can someone help me to figure out how to compare title and category name of a wordpress post…

    For example if category name is “wordpress” and title is “Blogs with wordpress” I like to have an output “Match”

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • For use in the loop:

    <?php
    $cats = wp_get_post_categories($post->ID);
    if ($cats) {
      $first_cat = $cats[0];
      $category = get_category($first_cat);
      if ( strpos(strtoupper($post->post_title), strtoupper($category->name)) ) {;
        echo 'Match';
      }
    }
    ?>

    Thank you. But here we are checking only one category, right?

    Can you please help me to find out how to check this for all categories…

    All the categories are in the $cats array, just use foreach structure to check each element.

    <?php
    $cats = wp_get_post_categories($post->ID);
    if ($cats) {
      foreach ($cats as $cat) {
        $category = get_category($cat);
        if ( strpos(strtoupper($post->post_title), strtoupper($category->name)) ) {;
          echo 'Match';
        }
      }
    }
    ?>

    Thanks very much for your time.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Compare title and category of a wordpress post’ is closed to new replies.
Skip to toolbar