5

So I have created a custom post type and under that taxonomy named "Category" which serves for categories. How can I list all the categories from there?

2 Answers 2

3

If you just want to list them you can use the get_terms function:

$terms = get_terms( 'my_taxonomy' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
 echo '<ul>';
 foreach ( $terms as $term ) {
   echo '<li>' . $term->name . '</li>';

 }
 echo '</ul>';
}

Read the codex, it has a lot of examples: https://codex.wordpress.org/Function_Reference/get_terms

1

nm I should start my own question...

EDIT: I figured out what I was going to ask, but if anybody stumbles on this original question, here's what you can also do to just link to the categories after they are listed:

<?php
$terms = get_terms( 'nameofyourregisteredtaxonomygoeshere' );
$count = count( $terms );
if ( $count > 0 ) {
echo '<h3>Total Projects: '. $count . '</h3>';
echo '<ul>';
foreach ( $terms as $term ) {
    echo '<li>';
    echo '<a href="' . esc_url( get_term_link( $term ) ) . '" alt="'. esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
    echo '</li>';


}
echo '</ul>';
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.