0

is there any way to loop terms in a way they display in menu? Eg:

Let say I have 2 taxonomies in a post type - tax1 and tax2.

They are not hierarchical to each other.

If I have posts that have terms like this:

  • post 1 => tax1 = p1 ,tax2 = term1
  • post 2 => tax1 = p1 ,tax2 = term2
  • post 3 => tax1 = p1 ,tax2 = term3
  • post 4 => tax1 = p2 ,tax2 = termA
  • post 4 => tax1 = p2 ,tax2 = termB
  • ......etc

Can I use

if(tax1=p1) echo 'term1,term2, term3... in menu/widget'
elseif if(tax1=p2) echo 'termA,termB, termC... in menu/widget'

in my template to display the tax2 terms in the sidebar menu?

Somebody might wondering why I don't want to use parent-child terms. Well, this is because I expects have hundreds of term in tax2. If I use hierarchical taxonomy, that will be very long list in single taxonomy. I don't know whether WP able to process them or not.

1 Answer 1

0

Rather than relying on a literal menu, you could use wp_list_pages possibly to better effect and sort out by certain criteria if you wish, then foreach page, display terms. Here is a little function I wrote to spit out terms for a taxonomy passed to it:

// give taxonomy, will return link list to custom structure. 
function tax_link_list( $taxonomy ) {
$terms = get_the_terms($post->ID, $taxonomy);
if ($terms && ! is_wp_error( $terms ) ) {
    $term_links = array();
    foreach ($terms as $term) { 
        $term_links[] = '<a href="'.get_bloginfo('url').'/property-results/?'.$taxonomy.'='.$term->slug.'">'.$term->name.'</a>';
    }
    $term_space = implode(' ', $term_links);
    return $term_space;
}
}

In this example it is returning a list of links for a real estate search engine. You could have it simply return the text. To run this, I pass a taxonomy to the function and get a list of terms. Modified, this might work for you. Check out wp_list_pages as well. Good luck.

2
  • Correct me if I am wrong, cause I am not so good in coding. From your codes what I get is full list of terms from the taxonomy (which is tax2, from my example above) that I defined in the function? They will not grouped based on the tax1's term?
    – dev-jim
    Commented Jun 1, 2012 at 20:24
  • Right. You're going to write that function and edit it's output probably to something more along the lines of (in the foreach): $term_links[] = $term->name;. When you run the function (in page or wherever), you'll say: echo tax_link_list($tax1);
    – GhostToast
    Commented Jun 1, 2012 at 20:26

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.