WordPress.org

Make WordPress Core

Changeset 15677


Ignore:
Timestamp:
10/01/10 17:44:53 (4 years ago)
Author:
ryan
Message:

has_term(). Props ptahdunbar. fixes #12526

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

    r15590 r15677  
    249249 * @since 1.2.0 
    250250 * 
    251  * @uses is_object_in_term() 
     251 * @uses has_term() 
    252252 * 
    253253 * @param int|string|array $category Category ID, name or slug, or array of said. 
     
    255255 * @return bool True if the current post is in any of the given categories. 
    256256 */ 
    257 function in_category( $category, $_post = null ) { 
    258     if ( empty( $category ) ) 
    259         return false; 
    260  
    261     if ( $_post ) { 
    262         $_post = get_post( $_post ); 
    263     } else { 
    264         $_post =& $GLOBALS['post']; 
    265     } 
    266  
    267     if ( !$_post ) 
    268         return false; 
    269  
    270     $r = is_object_in_term( $_post->ID, 'category', $category ); 
    271     if ( is_wp_error( $r ) ) 
    272         return false; 
    273     return $r; 
     257function in_category( $category, $post = null ) { 
     258    return has_term( $category, 'category', $post ); 
    274259} 
    275260 
     
    972957} 
    973958 
     959 
     960/** 
     961 * Check if the current post has any of given category. 
     962 * 
     963 * @since 3.1.0 
     964 * 
     965 * @uses has_term() 
     966 * 
     967 * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for. 
     968 * @param int|object $post Optional. Post to check instead of the current post. 
     969 * @return bool True if the current post has any of the given categories (or any category, if no category specified). 
     970 */ 
     971function has_category( $category = '', $post = null ) { 
     972    return has_term( $category, 'category', $post ); 
     973} 
     974 
    974975/** 
    975976 * Check if the current post has any of given tags. 
     
    985986 * @since 2.6.0 
    986987 * 
     988 * @uses has_term() 
     989 * 
     990 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. 
     991 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) 
     992 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). 
     993 */ 
     994function has_tag( $tag = '', $post = null ) { 
     995    return has_term( $tag, 'post_tag', $post ); 
     996} 
     997 
     998/** 
     999 * Check if the current post has any of given terms. 
     1000 *  
     1001 * The given terms are checked against the post's terms' term_ids, names and slugs. 
     1002 * Terms given as integers will only be checked against the post's terms' term_ids. 
     1003 * If no terms are given, determines if post has any terms. 
     1004 * 
     1005 * @since 3.1.0 
     1006 * 
    9871007 * @uses is_object_in_term() 
    9881008 * 
    989  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. 
    990  * @param int|object $_post Optional. Post to check instead of the current post. (since 2.7.0) 
    991  * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified). 
    992  */ 
    993 function has_tag( $tag = '', $_post = null ) { 
    994     if ( $_post ) { 
    995         $_post = get_post( $_post ); 
    996     } else { 
    997         $_post =& $GLOBALS['post']; 
    998     } 
    999  
    1000     if ( !$_post ) 
     1009 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for. 
     1010 * @param int|post object Optional.  Post to check instead of the current post. 
     1011 * @since 2.7.0 
     1012 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). 
     1013 */ 
     1014function has_term( $term = '', $taxonomy = '', $post = null ) { 
     1015    $post = get_post($post); 
     1016 
     1017    if ( !$post ) 
    10011018        return false; 
    10021019 
    1003     $r = is_object_in_term( $_post->ID, 'post_tag', $tag ); 
     1020    $r = is_object_in_term( $post->ID, $taxonomy, $term ); 
    10041021    if ( is_wp_error( $r ) ) 
    10051022        return false; 
     1023 
    10061024    return $r; 
    10071025} 
Note: See TracChangeset for help on using the changeset viewer.