patiboh
5/27/2016 - 1:01 PM

WordPress - disply post categories & tags

WordPress - disply post categories & tags

<?php

global $post;
$post_cat  = get_the_category( $post );
$post_tags = get_the_tags( $post );
$post_tax_start = '';
$post_tax_end = '';

if($post_cat && !empty($post_cat) || $post_tags && !empty($post_tags)) {
	$post_tax_start = '<aside class="post-taxonomies">';
	$post_tax_end = '</aside>';
}

/* output opening <aside> tag if there are taxonomies, empty otherwise*/
echo $post_tax_start; 

if($post_cat && count($post_cat) >1):

	$categories = '';
	foreach ($post_cat as $index => $category) {
		$is_uncategorized = preg_match('/[U|u]ncategorized|[N|n]on classé/s',  $category->name);
		if(!$is_uncategorized) {
			$cat_link = get_term_link($category->name, 'category');
			$categories .= '<li><a href="'.$cat_link.'">'.$category->name.'</a></li>';
		}
	} 
	?>

	<ul class="post-categories"><?= $categories ?></ul>

<?php
endif;
	
if ($post_tags && !empty($post_tags)) :
	$tags = '';
	foreach ($post_tags as $index => $tag) {
		$tag_link = get_term_link($tag->name, 'post_tag');
		$tags .= '<li><a href="'.$tag_link.'">'.$tag->name.'</a></li>';
	} 
	?>

	<ul class="post-tags"><?= $tags ?></ul>

<?php
endif;

/* output closing </aside> tag if there are taxonomies, empty otherwise*/
echo $post_tax_end;