Here, like Tom does well, I am just noting some code thinking in case it gets fuzzy later.

Maybe this is an edge case that almost no one will face. A project I am working on now has custom post types I inject via the handy plugin. As it turns out, maybe for the theme I am trying, when displayed, it does not show any of the taxonomies it is associated with. A theme of course could not anticipate what I am doing,

Normally, this is something one puts into a custom theme template; often a theme has some kind of “get post meta” function built in leveraging the function get_the_term_list().

But in the project I am fleshing out, I am not formatting output as theme-templates, but user configurable ones they edit as content blocks (with placeholders populated by shortcodes).

I searched in vain for some plugins that would offer a shortcode that might then output the taxonomy a post/content type belonged to. It might be out there, so I came up shy.

So here is where I rolled up my sleeves… no they were already rolled, but wrote my own shortcode functions, which can go into a plugin or just added to a site with the Code Snippets plugin. My goal is here is something independent of the theme itself.

First is a [taxlist] a shortcode for displaying the category-like taxonomy a post belongs to, producing an HTML ordered list of the taxonomies with links to view their archives. It offers optional parameters:

  • taxonomy – the slug name for a custom taxonomy; default is category
  • id – optional if used on a different context than within a given post to target a specific content item (I did not need this but thought it useful). Defaults to the item in view
  • list_type – it might be useful sometimes to have an ordered HTML list, default is ul
  • class – optional CSS class name for formatting purposes
  • description – whether to include the taxonomy description or not
// shortcode to get an HTML list of taxonomy terms for a given post ID
add_shortcode("taxlist", "mytools_taxlist");

function mytools_taxlist( $atts )  {
  	extract(shortcode_atts( array( 
  		"taxonomy" => 'category', 
  		"id" => get_the_id(), 
  		"list_type" => 'ul',
  		"class" => 'taxlist',
  		"description" => false  
  	), $atts ));
  	
  	$terms = get_the_terms( $id, $taxonomy);
  	
  	if ( ! empty( $terms ) && !is_wp_error( $terms ) ) {
    	$output = '<' . $list_type . '" class="' . $class . ' ">';
    	 foreach ($terms as $term) {
    	 	$descrip = ( $description && !(empty($term->description) ) ) ? ' - ' . $term->description . '' : '';
    	 	$output .= '
  • ' . $term->name . '' . $descrip . '
  • '; } $output .= ''; return ($output); } }

    I made a second function for a case where the taxonomy is more like tags, and the display is like a single line string, separated by default, commas. This ends up being almost a direct path to the built in function.

    // shortcode to get something like a comma separated list of terms (e.g. for tags) for a given post ID
    add_shortcode("taxstring", "mytools_taxstring");
    
    function mytools_taxstring( $atts )  {
      	extract(shortcode_atts( array( 
      		"taxonomy" => 'category', 
      		"id" => get_the_id(), 
      		"before" => '',
      		"sep" => ', ',
      		"after" => ''  
      	), $atts ));
      	
      	$terms = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
      	
      	return ($terms);
    }
    

    Again, this could be of little use to anyone, but what I so enjoy about WordPress is often I can find plugins to do what I need, if not there is likely custom code swimming out there in StackExchange (ir blogs, those old time things), and if that fails, I can cobble my own.

    And that all feels like my kind of space to build in.


    Image Credits: When thinking of this post, my associative brain cells went to the old “What’s My Line?” TV show. The Wikimedia Commons image was perfect- Whats My Line original television panel 1952.JPG licensed as public domain with some long legged reasoning (age, lack of copyright, probably contestable, sue us all). I Photoshopped in the change of Line to Taxonomy, shoving wood around behind it, and changed the name labels. I may have spent more time futzing on the image than writing the post, both of which took longer than writing the code.

    Black and white 1950s image of a panel on TV show with sign in front "What's My Taxonomy?"
    If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
    Become a patron at Patreon!
    Profile Picture for CogDog The Blog
    An early 90s builder of web stuff and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as @cogdog@cosocial.ca

    Comments

    Leave a Reply to Tom Cancel reply

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