WordPress Snippets at WPcustoms

Add download category and tag to post class

These two functions will add the download category and the download tag slug to the post_class. A good helper for custom css settings if a download item has a specific category like ‘featured’ or something similar.


/**
 * Snippet Name: Add download category and tag to post class
 * Snippet URL: http://www.wpcustoms.net/snippets/add-download-category-tag-post-class/
 */
 function download_category_id_class($classes) {
   global $post;
   $categories = get_the_terms( $post->ID, 'download_category' );
   if( $categories ) {
	    foreach( $categories as $category ) {
	        $classes[] = $category->slug;
	    }
	}
	return $classes;
}
add_filter('post_class', 'download_category_id_class');

function download_tag_id_class($classes) {
   global $post;
   $tags = get_the_terms( $post->ID, 'download_tag' );
   if($tags) {
	    foreach( $tags as $tag ) {
   	     $classes[] = $tag->slug;
		}
	}
   return $classes;
}
add_filter('post_class', 'download_tag_id_class');