0

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?

1 Answer 1

2

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!

3
  • 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, 2012 at 5:57
  • What is your custom link? BTW, if it doesn't related to this question, you should ask it separately.
    – Anh Tran
    Sep 5, 2012 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, 2012 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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