Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active January 5, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/8f701bf65efab19cbae7 to your computer and use it in GitHub Desktop.
Save igorbenic/8f701bf65efab19cbae7 to your computer and use it in GitHub Desktop.
Display the Terms of a Custom WordPress Taxonomy |
<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>
<?php
$the_terms = get_the_term_list( get_the_id(), 'food_type', __( "Serving" ) . "<ul><li>", "</li><li>", "</li></ul>" );
?>
<div class="restaurant_food_types">
<?php echo $the_terms; ?>
</div>
<?php
/**Example:
* Terms: fish, meat, vege
* Step 1:
* Serving: <ul><li>fish
* Step 2
* Serving <ul><li>fish</li><li>meat
* Step 3
* Serving <ul><li>fish</li><li>meat</li><li>vege
* Step 4
* Serving <ul><li>fish</li><li>meat</li><li>vege</li></ul>
?>
<?php the_terms( $id, $taxonomy, $before, $sep, $after ); ?>
<div class="restaurant_food_types">
<?php the_terms( get_the_id(), 'food_type', __( "Serving" ), "," ); ?>
</div>
<?php
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
?>
<?php
// returned IDs
array( 30, 23, 34 );
// returned names
array( 'Food', 'Vege', 'Meat' );
?>
<?php
function my_terms( $post_id, $taxonomy, $echo = true, $description = true ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
$output = "";
//If there are any terms associated with the $post_id
if( count( $terms ) > 0 ) {
$output .= "<ul>";
foreach( $terms as $term ) {
$output .= "<li> . $term->name;
if( $description ) {
$output .= "<small>" . $term->description . "</small>";
}
$output .= "</li>";
}
$output .= "</ul>";
}
if( $echo ) {
echo $output;
} else {
return $output;
}
}
?>
<?php
my_terms( $post_id, 'food_type', true, true ); //Show the list with the description
my_terms( $post_id, 'food_type', true, false ); //Show the list without the description
$food_types = my_terms( $post_id, 'food_type', false, true ); //Return the list with the description
echo $food_types; //Display the list
$food_types_no_desc = my_terms( $post_id, 'food_type', false, false ); //Return the list without the description
echo $food_types_no_desc; //Display the list
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment