Categories
Code WordPress

Simple Taxonomies Formatting — Improve the Plugin's Code Output

Making the Simple Taxonomies WordPress Plugin Semantic

I’ve been using Joost de Valk’s Simple Taxonomies plugin for a couple of projects, and I’ve been very disappointed by the formatting of the terms output code.

When configuring the plugin, you have the option of choosing “Add terms to the end of posts” or “Add terms to the end of excerpts.” If you do, you get a <div> and a couple of spans. Not very semantic. Also, the code uses an #id, instead of a .class, meaning that if you have more than one post on a page with taxonomies, it no longer validates.

Simple Taxonomies uses terms, so let’s make a list of them!

Here’s a way to reformat the code and prevent overwriting in future plugin updates. We’re going to strip the code and use a definition list instead (<dl>). Definition lists in HTML have a term and description; just as a custom taxonomies creates a taxonomy and its terms.

Add this code to your theme’s functions.php file:

// Remove the default Simple Taxonomies Formatting
remove_filter(   'the_excerpt', 'yoast_simple_taxonomies_excerpt_filter', 10, 1 );
remove_filter(   'the_content', 'yoast_simple_taxonomies_content_filter', 10, 1 );

// Make your own filter
function custom_simple_taxonomies_filter( $content, $type ) {
   $opt  = get_option('yoast_simpletax');
   $output = '';
   foreach ( $opt['taxonomies'] as $taxonomy ) {
      $filter = false;
      if ( $type == 'content' && $taxonomy['filter'] ) {
         $filter = true;
      } else if ( $type == 'excerpt' && $taxonomy['filterexcerpt'] ) {
         $filter = true;
      }
      if ( $filter ) {
         // For each term, create a dd wrapper (Definition Description)
         $terms = get_the_term_list( $post->ID, $taxonomy['name'], $taxonomy['label'].':</dt><dd>', ',</dd><dd>', '</dd>' );
         if ($terms)
            // For each taxonomy, create a dt wrapper (Definition Term)
            $output .= "t".'<dt class="taxonomy-'.$taxonomy['name'].'">'.$terms."n";
      }
   }
   if ($output != '') {
      // Wrap the shebang in a dl (Definition List)
      $content .= '<dl class="yoast-taxonomy">'."n".$output."n".'</dl>'."n";
   }
   return $content;
}

// Recreate the functions
function custom_simple_taxonomies_content_filter( $content ) {
   return custom_simple_taxonomies_filter( $content, 'content' );
}
function custom_simple_taxonomies_excerpt_filter( $content ) {
   return custom_simple_taxonomies_filter( $content, 'excerpt' );
}
// Add the filters back on the content
add_filter(   'the_excerpt', 'custom_simple_taxonomies_excerpt_filter', 10, 1 );
add_filter(   'the_content', 'custom_simple_taxonomies_content_filter', 10, 1 );

So, just paste the code into your functions.php file, and then you just need to style the output so that it looks the same (or not), and you’re all set.

Did this make your life easier? Was this information worth a buck?
Donate with PayPal (Much appreciated!)

By Zack Katz

Zack Katz is the founder of GravityKit and TrustedLogin. He lives in Leverett, Massachusetts with his wife Juniper.

2 replies on “Simple Taxonomies Formatting — Improve the Plugin's Code Output”

hey zack,

nice solution but I have another question and yoast doesn’t seem to be answering: would it be hard to integrate shortcodes?
I’d love to be able to show certain tags only on certain posts, so manually inserting shortcodes would be perfect…

btw. do you have a sample page where you use this plugin? I am curios how different people are using it…

I’m looking for the same thing, did you find something?
I also want to display related post based on taxonomy termstags… with shortcode!

Comments are closed.