has_term(). Props ptahdunbar. fixes #12526

git-svn-id: http://svn.automattic.com/wordpress/trunk@15677 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-10-01 17:44:53 +00:00
parent a4167676ea
commit 0449fee5da
1 changed files with 47 additions and 29 deletions

View File

@ -248,29 +248,14 @@ function get_the_category_list( $separator = '', $parents='', $post_id = false )
* *
* @since 1.2.0 * @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|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) * @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. * @return bool True if the current post is in any of the given categories.
*/ */
function in_category( $category, $_post = null ) { function in_category( $category, $post = null ) {
if ( empty( $category ) ) return has_term( $category, 'category', $post );
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;
} }
/** /**
@ -971,6 +956,22 @@ function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
echo apply_filters('the_terms', $term_list, $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. * 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 * @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 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) * @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). * @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 ) { function has_tag( $tag = '', $post = null ) {
if ( $_post ) { return has_term( $tag, 'post_tag', $post );
$_post = get_post( $_post ); }
} else {
$_post =& $GLOBALS['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; 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 ) ) if ( is_wp_error( $r ) )
return false; return false;
return $r; return $r;
} }