0

I need some help. I would like to make a custom taxonomy archive that shows a grid of the terms(categories) in the taxonomy. I know how to accomplish the grid portion, but I'm having trouble with the main function of displaying the terms. I feel like I've seen this done so many times, but not that I'm trying to accomplish it, I can't.

As an example, this page shows how I want the taxonomies to look - including title, featured image, post count of terms, etc.

Right now I can't even get the terms to show up as links to the posts within the term. And if I can accomplish that, how do I go about styling the pieces?

I would appreciate some guidance with this.

2 Answers 2

0

To retrieve all the terms in a taxonomy, you can make use of get_terms

Here is an example from that page to retrieve the term names of a custom taxonomy

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

     }
     echo "</ul>";
 }

Here is a list of what is returned by get_terms that you can work with

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

You have to play around with what you exactly need. The only thing is, the link to the terms is not returned, so you'll need to work with get_term_link to get the link.

Here is an example from that page

$terms = get_terms( 'species', 'hide_empty=0' );

echo '<ul>';

foreach ( $terms as $term ) {

    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );

    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }

    // We successfully got a link. Print it out.
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}

echo '</ul>';

This should kick-start you in achieving your goal

As for styling, this is something that you have to sort out by yourself as this doesn't fall into scope of this site.

7
  • Thank You Pieter! This helps alot. :) :) Great tutorial. Now I just need to apply styles to specific posts. I found another tutorial that is close, but I'll have to figure how to apply it to taxonomies and not posts :\
    – streetfire
    Commented Jul 19, 2014 at 13:17
  • Hi, one more thing. The list only shows the tax terms with posts in them. Is there a way I can also display the empty terms?
    – streetfire
    Commented Jul 19, 2014 at 18:17
  • check update. added extra code Commented Jul 19, 2014 at 18:28
  • Hi, that doesn't seem to be working for me.
    – streetfire
    Commented Jul 19, 2014 at 18:55
  • Should work. Check the codex examples as well. Also, "not working" is broad. What do you mean by not working Commented Jul 19, 2014 at 19:07
0

Update... I have gotten the following to work well :)

    <?php

$terms = get_terms( 'industrygroups' );

echo '<div class="row">';

foreach ( $terms as $term ) {

    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );

    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }

    // We successfully got a link. Print it out.
    echo '<div class="col-lg-3"><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></div>';
}

echo '</div>';

?>

Your Answer

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

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