Display Custom Taxonomy for Custom Post Type with Term Link

Advertisement

Here’s another WordPress snippet to display custom taxonomy for custom post type, pretty straight forward, first display lists of terms associated in the current post type, second get the term link by using get_term_link() builtin function and if things work without any error or issue then continue and display the link.


	// add separator for two or more terms
	$separtor = ' ';

	// get lists of term associated in the current post type
	$terms = get_the_terms( get_the_ID(), 'portfolio-category' );
	if( $terms ) {
		foreach ( $terms as $term ) {
			// get term link, the function name explain it
			$term_link = get_term_link( $term, 'portfolio-category' );

			// the function explain its purpose
			if( is_wp_error( $term_link ) )
				continue;

			// display term name with link
			echo $separtor . '<a href="'. $term_link .'">'. $term->name . '</a>';
			$separtor = ', ';
		}
	}

Happy Coding ^_^

Advertisement