Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display a combined listing of all terms in 2 different taxonomies WordPress

  • SOLVED

I'm basically looking to duplicate this: http://www.ted.com/talks/tags

I assume this is going to involve some kind of customised wp_list_categories function.

This page is going to be called 'Statistics'

- We have 2 taxonomies: taxonomy1 and taxonomy2.
- We want to show an archive of all terms used in taxonomy1 and taxonomy2
- Adding the (count) would be great, but not absolutely necessary.

Then:

- We'd like to add a class of either taxonomy1 or taxonomy2, so each can be styled separately, if necessary
- When you click on an item in the list, you'll need to be sent to /statistics/term/termname - that would be the case regardless of which taxonomy the term is in. We'd then have code on that /statistics/term page to list all terms that fall under that taxonomy term.

Answers (4)

2011-10-17

Julio Potier answers:

Hello

Did you try something for the moment, or do we have to do this from scratch ?

I've got my idea on this ... Do you got 2 tag taxo ? 2 cat taxo ? 1 / 1 ?

Thank you


Jon comments:

The site's all set up and functional, though I can't give out live access.

Taxonomies are working, and I know what I'm doing, though my PHP is limited. I'm not looking for complete handholding - I can get your code into a page and displaying, but looping through the taxonomies is the bit I'm finding difficult!


Jon comments:

Oh, and one is a cat taxonomy, the other is a tag taxonomy. The cat doesn't have to be hierarchical though.

There is also no overlap between the two, so no terms show up in both taxonomy1 and taxonomy2.

Thanks!


Julio Potier comments:

Ok so here comes my code :

$all = get_terms( array( 'post_tag', 'category' ) );
$clean = array();
foreach( $all as $one ) {
$clean[$one->slug]['slug'] = $one->slug;
$clean[$one->slug]['name'] = $one->name;
$clean[$one->slug]['count'] = $one->count;
}
sort( $clean );
$letter = '';
foreach( $clean as $one ) {
$old_letter = $letter;
$letter = strtoupper( $one['name'][0] );
if( $letter != $old_letter ) {
echo '<h2>' . $letter . '</h2>';
}

echo '<a href="/statistics/term/' . $one['slug'] . '">' . $one['name'] . ' (' . $one['count'] . ')</a>';
echo '<br />';
}


My tes page : [[LINK href="http://demo.boiteaweb.fr/statistics"]]http://demo.boiteaweb.fr/statistics[[/LINK]]

See you soon !


Jon comments:

Thanks Julio - OK - this doesn't pull the taxonomy1 and taxonomy2 terms.

I'm also stuck in the middle of yours and Ivaylo's posts (below). His handles the custom taxonomies, but doesn't order them by letter like yours!

A solution that does both, plus the requirements in my reply to Ivaylo below, would be ideal?


Jon comments:

"doesn't pull the taxonomy1 and taxonomy2 terms" = pulls the regular list of cats, not my 2 custom taxonomies.


Julio Potier comments:

Just to be sure : did you modify the first line :
$all = get_terms( array( 'post_tag', 'category' ) );
and fill your term in ?


Jon comments:

Ah, my mistake completely - sorry! Thanks for the hint ;)

OK, that works well - now I just need to:

1) Can we limit to only terms in taxonomy1 and taxonomy2 used by custom post type: 'statistics'
3) And give me an idea of how I can tell an archive view to appear at /statistics/term/termnamehere


Julio Potier comments:

1) Can we limit to only terms in taxonomy1 and taxonomy2 used by custom post type: 'statistics'

<em>I can not do this. Or, i have to make my own request with $wpdn->query().</em>

3) And give me an idea of how I can tell an archive view to appear at /statistics/term/termnamehere

<em>I do not understand sorry.</em>


Jon comments:

I posted a reply to Ivaylo below, but how do you think this will work with $wpdn->query() ?

2011-10-17

Utkarsh Kukreti answers:

"We'd then have code on that /statistics/term page to list all terms that fall under that taxonomy term." You already have this, or want this implemented too?

Do you also want them classified by letters?


Jon comments:

It depends on the amount of work involved, to be honest, and how straightforward the first set of requirements are.

For this question specifically, I'm assuming that a customised loop will take care of this, but I'd need a few pointers on how to set this up so that the archive template shows up and the URLs are pointing in the right place. Does that make sense?!


Jon comments:

Oh, and for classification by letter: ideally yes. Again, depends on time constraints and how complex the first stage of this is.


Utkarsh Kukreti comments:

Here's a start

$terms = get_terms(array('tax1', 'tax2'));
foreach($terms as $term) {
echo "<a href='/statistics/term/" . $term->slug . "'";
echo "class='" . $term->taxonomy . "'>";
echo $term->name;
echo "</a>";
}


Utkarsh Kukreti comments:

For term count:

$terms = get_terms(array('tax1', 'tax2'));
foreach($terms as $term) {
echo "<a href='/statistics/term/" . $term->slug . "'";
echo "class='" . $term->taxonomy . "'>";
echo $term->name;
echo "(". $term->count . ")";
echo "</a>";
}

2011-10-17

Ivaylo Draganov answers:

Hello,

here's a function that will do what you basically need:

function custom_list_terms( $taxonomies = array( 'category' ) , $args = array() ) {

// vars
$r = '';

// get all terms
$terms = get_terms( $taxonomies, $args );

// if there are any temrs
if ( $terms ) {

// begin building output
$r .= '<ul class="term-archive">';

// loop terms
foreach ($terms as $term) {

// HTML for each term
$r .= '<li class="' . $term->taxonomy . '"><a href="/statistics/term/' . $term->slug . '" title="' . sprintf( __( 'View all post filed under %s' ), $term->name ) . '">' . $term->name . '</a> (' . $term->count . ')</li>';

}

$r .= '</ul>';

// echo/return the result
echo $r;

}

}


By default it will pull categories, so you have to pass your taxonomies as an argument:

<?php custom_list_terms( array( 'tax1', 'tax2' ) ); ?>


Jon comments:

Wow - this seems to work perfectly to list all the terms, as far as I can tell - a great start, thanks!

For the other requirements:

1) Can we limit to only terms in taxonomy1 and taxonomy2 <strong>used by custom post type: 'statistics'</strong>
2) Could you extend this to classify the terms by letter (A, B, C, D etc)
3) And give me an idea of how I can tell an archive view to appear at /statistics/term/termnamehere

No need for pagination on the archive page.

I think if we have those 3 requirements sorted, that would be everything I need!


Jon comments:

Sorry for not mentioning the custom post type earlier, too. Completely overlooked that bit, but it should hopefully be a simple tweak? :)


Ivaylo Draganov comments:

1) This is not so straight-forward (well, someone else may come up with a solution). I suggest you limit the use of these custom taxonomies only to the custom post types.

2) Borrowing some code from Julio Potier, here's an updated version of the function:
[[LINK href="http://pastebin.com/D5yqqzgc"]]http://pastebin.com/D5yqqzgc[[/LINK]]

3) Try passing the archive slug when registering the taxonomies, something like this:


register_taxonomy(
'genre',
array( 'book' ),
array(
'rewrite' => array(
'slug' => 'statistics/term'
'with_front' => false
),
)
);


I'm not sure whether it will work with the same slug for both taxonomies though... Then create a template file named <em>taxonomy-taxonomy1.php</em> and tailor it to your needs. By default it will use archive.php or index.php ( [[LINK href="http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display"]]See the Codex[[/LINK]] ).


Jon comments:

Hmmm...

1) OK - do you think it's at all doable, possibly based on what Julio mentioned above? Even if we increase the budget to handle this as well?

2) Doesn't seem to work, but Julio's code does. http://pastebin.com/sMGytEnY

3) One of those taxonomies is only assigned with the 'statistics' post type, but the other is shared with another post type and so I think interfering with the slug would affect that other post type too. Maybe we could test for the last URL segment?


Ivaylo Draganov comments:

1) It is doable, but only using custom queries as Julio suggested and this falls out of my skills scope

2) Hm, I just tested it and it works as expected. I made some enhancements to the code, please try again:
[[LINK href="http://pastebin.com/D5yqqzgc"]]http://pastebin.com/D5yqqzgc[[/LINK]]

** don't forget to pass the taxonomies as parameters when calling the function:

<?php custom_list_terms( array( 'taxonomy1', 'taxonomy2' ) ); ?>


3) The URL setup won't affect post types, the problem is that WP needs to have a separate base for URL different content types. Two taxonomies can't share the same base ('statistics/term') because WP would not know what to serve. I think that this is also doable and some of the more experienced folks here might give you a solution. I'd suggest going for distinct bases - 'statistics/taxonomy1' and 'statistics/taxonomy2' if that's acceptable


Jon comments:

Hi Ivaylo - sorry for the delay!

1) I managed to avoid having to limit this to only the 'statistics' custom post type - so that's all OK for now!

2) Works perfectly now!

3) No problem - I think I can get that sorted now.

Just one last related question: is there any way that I can output a summary for each of the terms output in this list - not sure how I would include the following code so that it shows the correct summary for each. It works perfectly on the single page for each item, but I want to display the summary in the list as well:

<?php
$cat = $wp_query->get_queried_object();
$category_id = $cat->term_id;
if (function_exists('get_terms_meta'))
{
$new_value = get_terms_meta($category_id, 'mysummary', true);
}
?>


Not sure how to integrate it with the code you put in the pastebin you shared a second ago?!


Ivaylo Draganov comments:

1) That's a wise choice IMO :--)

2) I'm glad it works (props to Julio for the initial idea of letter sorting)

3) As far as I can see this function requires the term ID in order to retrieve the summary, so there should be no problem to use it with my code as we have the terms IDs available. Please see the updated code with an example of how to fetch the summary and append it to the markup:
[[LINK href="http://pastebin.com/D5yqqzgc"]]http://pastebin.com/D5yqqzgc[[/LINK]]

If there's anything unclear don't hesitate to ask.


Jon comments:

Perfect! That's everything I need. Thanks to you (and Julio) for getting me there!

2011-10-17

Kannan C answers:

This is what you are looking for?

echo '<ul>';
$args_list = array(
'taxonomy' => 'name_of_your_taxonomy', // Registered tax name
'show_count' => true,
'hierarchical' => true,
'echo' => '0',
);
echo wp_list_categories($args_list);
echo '</ul>';

or

wp_tag_cloud( array( 'taxonomy' => 'name_of_your_taxonomy', format => 'list' ) );


Jon comments:

Thanks for your response, but there was quite a long list of requirements in my question, hence the $70 price. Quick glance at this looks like it's just a ist of cats and a tag cloud, which obviously doesn't do the majority of what I need - sorry.