Home / posts / Display taxonomy terms in an unordered list
  • How to Display taxonomy terms in an unordered list

    display-taxonomy-terms-in-an-unordered-list

    Adding this snippet to your wordpress theme will list the custom taxonomies that you registered for a custom post type.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    		<?php
    			$terms = get_the_term_list( $post->ID, 'TAXONOMY_NAME',',',',','');
    			$terms = explode(',',$terms);
    			echo '<ul>';
    				for($i = 0;$i<sizeof($terms);$i++){ ?>
    					<li><? echo $terms[$i]; ?></li>
    				<? }
    			echo '</ul>';
    		?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_the_term_list, _n,
  • You can actually accomplish this with far less code. get_the_term_list() allows for before and afters and wraps to be set as parameters, so you could accomplish the same thing using one line of code, e.g.:

    [php]&lt;?php echo get_the_term_list( $post-&gt;ID, ‘TAXONOMY_NAME’, ‘&lt;ul&gt;&lt;li&gt;’,’&lt;/li&gt;&lt;li&gt;’,’&lt;/li&gt;&lt;/ul&gt;’); ?&gt;[/php]

    • you are right, good call Drew, Ill updated the post with the new version.