Add Content To Posts In Specific Taxonomy Term ( Conditional Tag )

This conditional tag enables you to target single entries assigned to a specific term for a custom taxonomy.

In this case, the taxonomy type slug is industry-type and the term is named long distance movers

add_action( 'loop_start', 'term_conditional_term_slug' );
function term_conditional_term_slug() {

$term = is_object_in_term( get_the_ID(), 'industry-type', 'long-distance-movers' );
if ( $term AND is_singular('industry') ) {
echo esc_html__( 'Hello World Using Term Slug','text-domain' );
    }

}

This code prints the message Hello World on all single posts using the term long-distance movers.

It also uses the is_singular conditional tag with the custom post type named industry.

You could also write the code like this which uses the term slug 9 ( NOT wrapped in single or double quotes ( if you do this, it won’t work ):

add_action( 'loop_start', 'term_conditional_term_slug' );
function term_conditional_term_slug() {

$term = is_object_in_term( get_the_ID(), 'industry-type', 9 );
if ( $term AND is_singular('industry') ) {
echo esc_html__( 'Hello World Using Term Slug','text-domain' );
    }

}

The above 2 code snippets work in your themes functions.php file however you can also use code in your single template.

Usage in Single Template

You could also remove the conditional tag from the code for the single template and add the code to your custom post type single template which in this case is named single-industry.php, industry being the name of the CPT.

add_action( 'loop_start', 'term_conditional_term_slug_single_template' );
function term_conditional_term_slug_single_template() {

$term = is_object_in_term( get_the_ID(), 'industry-type', 47 );
if ( $term ) {
echo esc_html__( 'In Moving Companies Single Template Term ID 47','text-domain' );
    }

}

Exclude Post From Function

If you don’t want the content printed on a specific entry, add a condition tag to te function like this with the ID for the post which in this case is 10.

add_action( 'loop_start', 'term_conditional_long_distance_movers' );
function term_conditional_long_distance_movers() {

if ( is_single('10') )
    return;

$term = is_object_in_term( get_the_ID(), 'industry-type', 'long-distance-movers' );
if ( $term AND is_singular() ) {
echo esc_html__( 'In Term Long Distance Movers','text-domain' );
    }

}

Note : You will need to swap out either the term slug or term ID with your own as well as the CPT name, if used. And, swap out the text-domain with your themes text domain.

Demo Video

Shows you how to use the is_object_in_term function to create a conditional tag which targets posts in specific terms for a specific taxonomy for any post type including custom posts types.


Comments

6 responses to “Add Content To Posts In Specific Taxonomy Term ( Conditional Tag )”

  1. James Hahn II Avatar
    James Hahn II

    Hi Brad, I’d like to use this code to add contact information into individual WooCommerce Product short descriptions. I’ve created a taxonomy called “vendor” using Custom Post Type UI and it’s working well.

    http://oiltizer.wpengine.com/vendor/purple-wave/
    http://oiltizer.wpengine.com/vendor/kruse-energy/

    Over and above adding words, I’d like to add the appropriate HTML for subheadings, div classes, href tel, etc. Can you help?

    Happy to pay the additional tutorial request fee if it’s required.

    1. James Hahn II Avatar
      James Hahn II

      Never mind. I figured it out.
      [code]
      add_action( ‘woocommerce_single_product_summary’, ‘add_my_text’ );
      function add_my_text() {
      if ( wc_get_product_class( $class = ‘purple-wave’ ) ) {
      echo ‘This is my extra text.’;
      }
      }
      [/code]

    2. Why can’t you add the HTML to the short description field manually?

      1. James Hahn II Avatar
        James Hahn II

        Needed a way to automatically add contact info and GravityForms based on vendors specified during bulk product uploads. The company adds and removes dozens and sometimes hundreds of product listings each month, so a manual process was out of the question. This automates the process and adds the appropriate vendor contact info and form. Example (adding forms this morning): http://oiltizer.wpengine.com/shop/1977-international-f2070a-dump-truck/

        1. James Hahn II Avatar
          James Hahn II

          Quick correction. Turns out my code above adds your “extra text” to all products. In my case, it added all vendors to all products. `has_term` is the correct function.

          [code]
          add_action( ‘woocommerce_product_meta_start’, ‘add_my_text_vendor1’ );
          function add_my_text_vendor1() {
          if ( has_term( ‘vendor1’, ‘vendor’ ) ) {
          echo ‘Additional text for the first vendor.’;
          }
          }

          add_action( ‘woocommerce_product_meta_start’, ‘add_my_text_vendor2’ );
          function add_my_text_vendor2() {
          if ( has_term( ‘vendor2’, ‘vendor’ ) ) {
          echo ‘Additional text for the second vendor.’;
          }
          }
          [/code]
          The second example here connected the dots for me.

          https://developer.wordpress.org/reference/functions/has_term/#user-contributed-notes

          [code]
          if( has_term( ‘jazz’, ‘genre’ ) ) {
          // do something
          }
          [/code]

          Hope this helps!

          1. I didn’t test this but normally you would use array() for multiple terms like this :

            [code]
            has_term(array(‘1′,’2′,’3’))
            [/code]

            1st parameter $term
            (string|int|array) (Optional) The term name/term_id/slug or array of them to check for.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.