Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have setup two custom taxonomies for a custom post type that I have created. The post type is called beer, and the two taxonomies are country and brewers.

I would like to list them like this.

 Country
 -->Brewers
 ----->Beers

I can pull the countries using this code.

    $terms_country = get_terms('country');
    foreach ($terms_country as $term_country) {
    echo "<h3 class=\"country-heading\" id=\"".$term_country->slug."\">";
    echo '<a href="/beers/country/' . $term_country->name . '">' . $term_country->name       . '</a>';
    echo "</h3>";

I need to query the country term to list the Brewers that have that taxonomy attached to the post as well.

share|improve this question
    
something like this? wordpress.stackexchange.com/questions/70831/… –  Mridul Aggarwal Nov 10 '12 at 8:41
    
That appears to get me the data when I dump it. How do I format it so that I can use it in a list? I am a PHP noob! –  dclardy Nov 11 '12 at 2:19
    
added a usage example as an answer –  Mridul Aggarwal Nov 11 '12 at 13:38
add comment

1 Answer

NOTE: This answer is supposed to be in addition to the answer here

The function used returns an array of all the items, so the best way to display them is to use a foreach loop. As a usage example

global $wpdb;
$result = $wpdb->get_col(....); //copy the line from the aforementioned answer

if($result) {
    echo '<ul>';
    foreach($result as $term) {
        echo '<li>' . $term . '</li>';
    }
    echo '</ul>';
}

In case you changed the get_col to get_results to query more details about the terms, the foreach loop changes just a little

    foreach($result as $term) {
        echo '<li>' . $term->name . ' with slug ' . $term->slug . '</li>';
    }
share|improve this answer
    
This only lists the first grouping. It is doing almost the same thing as wp_list_categories. How can I get the Brewers looped inside of this? –  dclardy Nov 12 '12 at 3:06
    
Maybe i didn't exactly get you, wp_list_categories lists all the terms. This however, lists the countries terms with attached posts that also have brewers attached –  Mridul Aggarwal Nov 12 '12 at 4:26
    
or are you looking for get_the_terms. It returns the terms in a specific taxonomy attached to a post –  Mridul Aggarwal Nov 12 '12 at 4:29
    
All of the posts are going to have both taxonomies attached so that doesn't really matter in this case. I need to list them as described in my first post. All countries then all brewers then the posts by brewer. –  dclardy Nov 12 '12 at 15:19
    
For that i think maybe you may loop through the posts of country & use get_the_terms to get all the brewers but there must be another more efficient way –  Mridul Aggarwal Nov 12 '12 at 18:08
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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