From 0449fee5da1c04831a89f264f0cbc9267fbb5f82 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 1 Oct 2010 17:44:53 +0000 Subject: [PATCH] has_term(). Props ptahdunbar. fixes #12526 git-svn-id: http://svn.automattic.com/wordpress/trunk@15677 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/category-template.php | 76 +++++++++++++++++++------------ 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 9ba14a8b4..3d2495e38 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -248,29 +248,14 @@ function get_the_category_list( $separator = '', $parents='', $post_id = false ) * * @since 1.2.0 * - * @uses is_object_in_term() + * @uses has_term() * * @param int|string|array $category Category ID, name or slug, or array of said. * @param int|object $_post Optional. Post to check instead of the current post. (since 2.7.0) * @return bool True if the current post is in any of the given categories. */ -function in_category( $category, $_post = null ) { - if ( empty( $category ) ) - return false; - - if ( $_post ) { - $_post = get_post( $_post ); - } else { - $_post =& $GLOBALS['post']; - } - - if ( !$_post ) - return false; - - $r = is_object_in_term( $_post->ID, 'category', $category ); - if ( is_wp_error( $r ) ) - return false; - return $r; +function in_category( $category, $post = null ) { + return has_term( $category, 'category', $post ); } /** @@ -971,6 +956,22 @@ function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after); } + +/** + * Check if the current post has any of given category. + * + * @since 3.1.0 + * + * @uses has_term() + * + * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for. + * @param int|object $post Optional. Post to check instead of the current post. + * @return bool True if the current post has any of the given categories (or any category, if no category specified). + */ +function has_category( $category = '', $post = null ) { + return has_term( $category, 'category', $post ); +} + /** * Check if the current post has any of given tags. * @@ -984,25 +985,42 @@ function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { * * @since 2.6.0 * - * @uses is_object_in_term() + * @uses has_term() * * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. - * @param int|object $_post Optional. Post to check instead of the current post. (since 2.7.0) - * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified). + * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) + * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). */ -function has_tag( $tag = '', $_post = null ) { - if ( $_post ) { - $_post = get_post( $_post ); - } else { - $_post =& $GLOBALS['post']; - } +function has_tag( $tag = '', $post = null ) { + return has_term( $tag, 'post_tag', $post ); +} - if ( !$_post ) +/** + * Check if the current post has any of given terms. + * + * The given terms are checked against the post's terms' term_ids, names and slugs. + * Terms given as integers will only be checked against the post's terms' term_ids. + * If no terms are given, determines if post has any terms. + * + * @since 3.1.0 + * + * @uses is_object_in_term() + * + * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for. + * @param int|post object Optional. Post to check instead of the current post. + * @since 2.7.0 + * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). + */ +function has_term( $term = '', $taxonomy = '', $post = null ) { + $post = get_post($post); + + if ( !$post ) return false; - $r = is_object_in_term( $_post->ID, 'post_tag', $tag ); + $r = is_object_in_term( $post->ID, $taxonomy, $term ); if ( is_wp_error( $r ) ) return false; + return $r; }