Last active
January 5, 2016 10:39
-
-
Save igorbenic/8f701bf65efab19cbae7 to your computer and use it in GitHub Desktop.
Display the Terms of a Custom WordPress Taxonomy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php the_terms( $id, $taxonomy, $before, $sep, $after ); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="restaurant_food_types"> | |
<?php the_terms( get_the_id(), 'food_type', __( "Serving" ), "," ); ?> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); | |
$terms = wp_get_post_terms( $post_id, $taxonomy, $args ); | |
?> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// returned IDs | |
array( 30, 23, 34 ); | |
// returned names | |
array( 'Food', 'Vege', 'Meat' ); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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