WordPress.org

Make WordPress Core

Ticket #6749: in_category.diff

File in_category.diff, 1.1 KB (added by Otto42, 10 years ago)

First attempt at allowing in_category to support arrays of inputs

  • wp-includes/category-template.php

     
    176176 *
    177177 * @since 1.2.0
    178178 *
    179  * @param int|string $category
     179 * @param int|string|array $category
    180180 * @return bool true if the post is in the supplied category
    181181*/
    182182function in_category( $category ) { // Check if the current post is in the given category
     
    185185        if ( empty($category) )
    186186                return false;
    187187
    188         $cat_ID = get_cat_ID($category);
    189         if ( $cat_ID )
    190                 $category = $cat_ID;
     188        $category = (array) $category;
     189       
     190        // get the ids of all the cats
     191        foreach ($category as $index => $cat) {
     192                if (is_string($cat)) {
     193                        $cat_ID = get_cat_ID($cat);
     194                        if ( $cat_ID ) $category[$index] = $cat_ID;
     195                }
     196        }
    191197
    192198        $categories = get_object_term_cache($post->ID, 'category');
    193199        if ( false === $categories )
    194200                $categories = wp_get_object_terms($post->ID, 'category');
    195         if ( array_key_exists($category, $categories) )
     201        if ( !empty(array_intersect($category, $categories) ) )
    196202                return true;
    197203        else
    198204                return false;