Make WordPress Core

Ticket #6590: has-tag.2.diff

File has-tag.2.diff, 1.2 KB (added by Otto42, 16 years ago)

Updated function to handle no tag case better

  • wp-includes/category-template.php

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