Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been using "if (is_single() && in_category('keyword') )" to display custom links on the sidebar. I was wondering if we have in_tag or any similar function for tags?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Here is the source of in_category function:

function in_category( $category, $post = null ) {
    if ( empty( $category ) )
        return false;

    return has_term( $category, 'category', $post );
}

You see, it's actually has_term. This is the same as has_category function.

For tags, there's no in_tag function, but you can use has_tag. It has the same functionality:

has_tag( $tag, $post );

Or you can use:

has_term( $tag, 'post_tag', $post );

where $post can be omitted (default is current post).

They're all the same!

share|improve this answer
    
I tried has_term before but it didn't work. However, has_tag works perfectly. I can't thank you enough. But I have a problem here. Is it possible to show custom links based on a generic tag? For example: I have tags like "iPhone News", "iPhone Apps" and I want to show custom links for "iPhone." I am using this code: <?php if (is_single() && has_tag('iphone') ) : ?> <ul><li><a href="http://...">Apple</a></li></ul><?php endif; ?> Replacing 'iphone' with 'iphone-news' shows custom links only on pages tagged with iPhone news. Once again, thanks for the help! –  Sid Sep 5 '12 at 5:57
    
What is your custom link? BTW, if it doesn't related to this question, you should ask it separately. –  Rilwis Sep 5 '12 at 8:05
    
These are links on the sidebar linking to "similar" pages, tags, categories, and so on. The function has_tag does it for me, but I was wondering if we can tweak it further. –  Sid Sep 5 '12 at 9:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.