Blog / plugins / WordPress has_term Conditional Tag Shortcode

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.

From the blog

Related Articles

WordPress Forum Plugins

A Guide to WordPress Forum Plugins: The Best Plugins & How To Manage Them

Author: Alex Denning
Last Updated: June 4th, 2020

If your WordPress site is designed to serve and engage with a community, you’ll need to install a WordPress forum plugin. Forum plugins add a platform where users can ask questions, provide answers, and join in discussions. It promotes engagement and gives users a sense of being part of a community. You can use it […]

Continue Reading

27+ WooCommerce Must Have Plugins for 2024: Improve Your Store

Author: Colin Newcomer
Last Updated: March 7th, 2024

What are the WooCommerce must have plugins that every store needs? If you found your way to this post, that's probably the issue you're dealing with right now. While the core WooCommerce plugin offers most of the basic functionality that you need for a store, pretty much all WooCommerce stores rely on dozens or even […]

Continue Reading

5 Free And Easy to Use WordPress Membership Plugins

Author: Cristian Antohe
Last Updated: May 22nd, 2019

Free and easy to use WordPress membership plugins are not a myth. Finding and getting started with them is simpler then you might think. Often website owners require visitors to sign up and become members of their platform before allowing access to the content. The solutions for WordPress users are a variety of membership plugins […]

Continue Reading

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.