Make WordPress Core


Ignore:
Timestamp:
02/28/2010 05:59:39 AM (14 years ago)
Author:
dd32
Message:

More powerful version is is_tax(). Props filosofo, scribu. Adds $term parameter. Fixes #11904

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r13318 r13486  
    222222
    223223/**
    224  * Whether the current page query has the given taxonomy slug or contains taxonomy.
     224 * Whether the current query is for the given taxonomy and/or term.
     225 *
     226 * If no taxonomy argument is set, returns true if any taxonomy is queried.
     227 * If the taxonomy argument is passed but no term argument, returns true
     228 *    if the taxonomy or taxonomies in the argument are being queried.
     229 * If both taxonomy and term arguments are passed, returns true
     230 *    if the current query is for a term contained in the terms argument
     231 *    which has a taxonomy contained in the taxonomy argument.
    225232 *
    226233 * @since 2.5.0
    227234 * @uses $wp_query
    228235 *
    229  * @param string|array $slug Optional. Slug or slugs to check in current query.
     236 * @param string|array $taxonomy Optional. Taxonomy slug or slugs to check in current query.
     237 * @param int|array|string $term. Optional. A single or array of, The term's ID, Name or Slug
    230238 * @return bool
    231239 */
    232 function is_tax( $slug = '' ) {
    233     global $wp_query;
    234 
    235     if ( !$wp_query->is_tax )
     240function is_tax( $taxonomy = '', $term = '' ) {
     241    global $wp_query, $wp_taxonomies;
     242
     243    $queried_object = $wp_query->get_queried_object();
     244    $tax_array = array_intersect(array_keys($wp_taxonomies), (array) $taxonomy);
     245    $term_array = (array) $term;
     246
     247    if ( ! ( $wp_query->is_tax || $wp_query->is_category || $wp_query->is_tag ) )
    236248        return false;
    237249
    238     if ( empty($slug) )
     250    if ( empty( $taxonomy ) )
    239251        return true;
    240252
    241     return in_array( get_query_var('taxonomy'), (array) $slug );
     253    if ( empty( $term ) ) // Only a Taxonomy provided
     254        return isset($queried_object->taxonomy) && count( $tax_array ) && in_array($queried_object->taxonomy, $tax_array);
     255
     256    return isset($queried_object->term_id) &&
     257            count(array_intersect(
     258                array($queried_object->term_id, $queried_object->name, $queried_object->slug),
     259                $term_array
     260            ));
    242261}
    243262
Note: See TracChangeset for help on using the changeset viewer.