From e3b575b617c4002d6b2f2db17e10e3e0b4b994d2 Mon Sep 17 00:00:00 2001 From: westi Date: Fri, 20 Jun 2008 13:52:18 +0000 Subject: [PATCH] Add new has_tag() template tag. Fixes #6590 props Otto42. git-svn-id: http://svn.automattic.com/wordpress/trunk@8131 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/category-template.php | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 121727674..69fc14d63 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -539,4 +539,41 @@ function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { echo $return; } +/** + * Check if the current post has the given tag + * + * @package WordPress + * @since 2.6 + * + * @uses wp_get_object_terms() Gets the tags. + * + * @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for + * @return bool True if the current post has the given tag, or any tag, if no tag specified + */ +function has_tag($tag = '') { + global $post; + $taxonomy = 'post_tag'; + + if ( !in_the_loop() ) return false; // in-the-loop function + + $post_id = (int) $post->ID; + + $terms = get_object_term_cache($post_id, $taxonomy); + if (empty($terms)) + $terms = wp_get_object_terms($post_id, $taxonomy); + if (empty($terms)) return false; + + if (empty($tag)) return (!empty($terms)); + + $tag = (array) $tag; + + foreach($terms as $term) { + if ( in_array( $term->term_id, $tag ) ) return true; + if ( in_array( $term->name, $tag ) ) return true; + if ( in_array( $term->slug, $tag ) ) return true; + } + + return false; +} + ?>