WordPress has_term Conditional Tag Shortcode

Cristian Antohe
Last Updated: 29/08/14

has_term checks if the current post has any of given terms. The first parameter can be an empty string. It expects a taxonomy slug/name as a second parameter and you can also pass a post ID or object as the third parameter.

It can be useful if you need to list a different message or apply a different css class to your post.

However, if you want to use this inside your content, it won’t work simply because you don’t run PHP code inside the WordPress content area.

has_term shortcode

This is where a simple shortcode can come a long way.

It’s a simple mapping of the has_term conditional tag to the [has-term] shortcode.
It gives you access to:

  • [has-term term=”term-name” taxonomy=”tax-name” value=”true” id=””]
        List this if post has term-name
    [/has-term]
  • [has-term term=”term-name” taxonomy=”tax-name” value=”false” id=””]
        List this if post doesn’t have term-name
    [/has-term]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/*
Plugin Name: WCK - Conditional Shortcode Based on has_term
Plugin URI: http://www.cozmoslabs.com
Description: Gives you access to the [has-term term="term-name" taxonomy="tax-name" value="true" id=""] List this if post has term-name [/has-term]  AND [has-term term="term-name" taxonomy="tax-name" value="false" id=""] List this if post doesn't have term-name [/has-term]
Author: Cristian Antohe
Version: 0.1
Author URI: http://www.cozmoslabs.com
*/
add_shortcode( 'has-term', 'wck_has_term_shortcode' );
function wck_has_term_shortcode( $atts, $content = false ){
	extract(
		shortcode_atts(	array( 'term' => '', 'taxonomy' => '', 'value' => true, 'id' => null,  ), $atts )
	);	
 
	if ( $value === 'true' ) $value = true;
	if ( $value === 'false' ) $value = false;
 
	if ( !$content || $term == '' || $taxonomy == '' ){
		return;
	}
 
	if ( has_term( $term, $taxonomy, $id ) && $value ){ 
		return $content;
	} elseif ( !has_term( $term, $taxonomy, $id ) && !$value ) {
		return $content;
	} else {
		return;
	}
}

You can also download it from here: Has Term Shortcode Plugin

Limitations

Currently there is no support for multiple term names to be passed inside term=”term-name” (similar to the actual has_term function), although that should be easy enough to implement in the plugin if you want to.

2 thoughts on “WordPress has_term Conditional Tag Shortcode

    Hi Christian I am a wordpress designer who has recently started developing. I have created my first plugin which is a very simple thing that basically just calls up CSS colored tables that I have created. I am currently just creating each CSS table by hand then making a shortcode out of it. What I would like to do is be able to allow the user to call up the shortcode and be able to alter things like the background color themselves. Something like this…
    [CSS Table background-color=”blue”]
    so rather than have a shortcode for each color they could just change the output like that and it would change the CSS. So can something like this be done with the has_term shortcode?

    Reply

    What if I want a conditional tag for posts within a taxonomy?

    For example, if I have a taxonomy called “Animals” (that includes groups like “dogs”, “cats”, “bears”, etc.) is there any way to identify posts within “animals”? In other words, something like:

    is_tax (‘animals’) && is_single ()….?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.