0

So I am attempting to build the first page in my navigation hierarchy to choose product categories.

I have a custom taxonomy called collections. I would like to create a page that loops through all terms in the collections taxonomy and displays the Term +link and description. Just about everything I have found besides the code below is creating a list of post from a particular taxonomy. That is not what I want yet. I want a master page that has each taxonomy term with a description that then links to the post that corresponds to that term.

For example if my taxonomy was transportation, terms may be car, boat, plane which would have a description explaining what a car boat or plane is. Then each term would link to a list of specific cars or whatnot.

I created a file called taxonomy-collections.php and put the following code in. But it is not doing the trick, can anyone suggest a solution to this problem?

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

$terms = get_terms( 'collections' );

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  term_description($post->ID,$term);

}

echo '</ul>';

?>

<?php endwhile; else : ?>
 <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

This is almost straight from the codex, so I'm guessing I am missing something.

currently the code has each term displayed as a list, but I do want to change it to a grid format if someone could help with that too.

So each term and description will be wrapped in a div and right aligned with the next.

1 Answer 1

0

You might want to check out this post, as your post is linked to that question. That should explain most of your question.

As for your code, move that outside the the loop. You were nearly there with your code. Check out get_terms in the codex to see which objects you can use that is returned by the function. You can use $term->description instead of term_description($post->ID,$term); to return the term' description. Modify your code to

$terms = get_terms( 'collections' );

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  $term->description; // This will return the description of the term

}

echo '</ul>';
6
  • Thanks, I made the page template and it works.. now to just get the items to link to the term permalink structure.
    – Jon C
    Commented Aug 24, 2014 at 8:23
  • Don't get you on this. With this code the terms are clickable and when you click on them they go to the term's taxonomy page? :-) Commented Aug 24, 2014 at 8:27
  • from the master list (above) it does go to the term taxonomy page. But the items in the term (my products) are not linking correctly. I need the structure to be collection>%term%>%product% but the products are not including the taxonomy in the permalink for some reason.
    – Jon C
    Commented Aug 24, 2014 at 13:49
  • OK, now I understand. You need to change your permalink structure when a page is being viewed from a specific term. Ask a new question, and include the code you've used to create your custom taxonomy with Commented Aug 24, 2014 at 13:57
  • ok. my taxonomy and CPT are created in PODS so that may make a difference. I'm scheduled to talk to someone who said he could help me a little, so we will see if he can solve the problem, otherwise I'll just hard code it. and convert everything to the new taxonomies again.
    – Jon C
    Commented Aug 25, 2014 at 0:03

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.