Make WordPress Core

Changeset 8131


Ignore:
Timestamp:
06/20/2008 01:52:18 PM (16 years ago)
Author:
westi
Message:

Add new has_tag() template tag. Fixes #6590 props Otto42.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

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