WordPress.org

Ready to get started?Download WordPress

Forums

is_category() ISSUE & REQUEST (4 posts)

  1. jtlowe22
    Member
    Posted 4 years ago #

    I am experiencing an issue with two categories:

    Name: 3-d
    slug: 3-d
    id: 42

    Name: Illustration-folio
    slug: illustration-folio
    id: 3

    When I am on a category archive page and '3-d' is the category, 'is_category(42)' and 'is_category(3)' evaluate to true.

    It would be nice to have separate functions for the type of category information being entered. For example, a function that only takes the category id. Or be able to accept arguments.

    If my issue is a result of me doing something incorrectly, please let me know.

    Thanks,
    Jordan

  2. jtlowe22
    Member
    Posted 4 years ago #

    function is_category ($category = '')
     {
         ...
          if ( in_array( $cat_obj->term_id, $category ) )
              return true;
          elseif ( in_array( $cat_obj->name, $category ) )
              return true;
          elseif ( in_array( $cat_obj->slug, $category ) )
              return true;
          ...
      }

    What I believe is happening is that when cat_id '3' is not found in '42' it looks for '3' in the current category name. Since it only looks for 3 anywhere in the $category variable, it finds 3 in 3-d evaluates this to true.

    I will look into this. Does anyone have suggestions?

    Thanks,
    Jordan

  3. jtlowe22
    Member
    Posted 4 years ago #

    This is also an inconvenience when you use is_category(cat_name) for a category name that shares a string with a similar category name. For example.

    using is_category('category') evaluates to true when on the archive page having 'category12' as its name.

  4. jtlowe22
    Member
    Posted 4 years ago #

    can check for both:

    if(is_category($cat_name) && is_category($cat_id)){
        ...
    }

Topic Closed

This topic has been closed to new replies.

About this Topic