Make WordPress Core

Ticket #6590: has-tag.diff

File has-tag.diff, 1.4 KB (added by Otto42, 15 years ago)

Add has_tag function.

  • wp-includes/category-template.php

     
    536536                echo $return;
    537537}
    538538
     539/**
     540 * has_tag() - Check if the current post has the given tag
     541 *
     542 * @package WordPress
     543 * @since 2.5.1
     544 *
     545 * @uses wp_get_object_terms() Gets the tags.
     546 *
     547 * @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for
     548 * @return bool True if the current post has the given tag, or any tag, if no tag specified
     549 */
     550function has_tag($tag = '') {
     551       global $post;
     552       $taxonomy = 'post_tag';
     553
     554       if ( !in_the_loop() ) return false; // in-the-loop function
     555
     556       $post_id = (int) $post->ID;
     557
     558       $terms = get_object_term_cache($post_id, $taxonomy);
     559       if (empty($terms))
     560               $terms = wp_get_object_terms($post_id, $taxonomy);
     561
     562       if (empty($tag)) return (!empty($terms));
     563
     564       $tag = (array) $tag;
     565
     566       foreach($terms as $term) {
     567               if ( in_array( $term->term_id, $tag ) )
     568                       return true;
     569               elseif ( in_array( $term->name, $tag ) )
     570                       return true;
     571               elseif ( in_array( $term->slug, $tag ) )
     572                       return true;
     573       }
     574
     575       return false;
     576}
     577
    539578?>