is_category() function

I have category which name is ’51m series’ (it has id 135). Why the expression is_category(51) is true on this category page? I think it should be false according to WordPress Codex. Is it a bug?
Wordpress 3.5.2

category.php code:

Read More
<?php get_header(); ?>

<?php
if (is_category(array(36, 63, 64, 65, 67))) {
    get_template_part('category', 'papers');        
}  elseif (is_category(array(5, 62, 61, 23, 17, 151))) {
    get_template_part('category', 'icons');
} elseif (is_category(array(25, 50, 51, 52, 53, 54, 55, 56, 58, 71))) {
    echo is_category(51);
    get_template_part('category', 'gallery-machine');
} else {
    get_template_part('category', 'gallery-frame');
}

?>

<?php get_footer(); ?>

51m series live You may see 1 (is_category(51)) on the of the page.

Related posts

1 comment

  1. My suspicion is that, for some reason, the name “51m series” or slug 51m-series is (or both are) matching is_category( 51 ).

    The is_category() function uses the PHP in_array() conditional to match against ID, slug, and name:

    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;
    

    The in_array() conditional appears to be returning true when 51 is found in the name/slug 51m series/51m-series. While both the name and slug conditional checks should be case-sensitive, none of the checks are strict. So the integer 51 is possibly being evaluated as a string against $cat_obj->name, and matching 51m Series (or against $cat_obj->slug, and matching 51m-series)

    Possible solutions:

    • Use category names/slugs that don’t begin with numbers
    • Query explicitly against category slugs rather than against category IDs

    This may also be a case where core needs to perform a strict check in in_array(), to differentiate between an ID integer and a name/slug string.

Comments are closed.