• Resolved Jorinde

    (@jorinde)


    Hello dear forum members,
    hope you can help me with my problem.

    I work with custom taxonomies. I have successfully registered them in my functions.php and used them to assign (multiple and hierarchically structured) values to my posts.

    My structure is similar to this example:
    Taxonomy: Habitat
    Terms:

    • A–America
    • a–USA
    • b–Canada
    • c–Mexiko
    • B–Europe
    • a–France
    • b–Spain
    • c–Italy

    etc.

    Now I want to display them via my single.php-Template. To do so, I used
    <?php the_terms( $post->ID, 'habitat', '<li> <strong>Habitat:</strong> ', ' ','</li>' ); ?>
    This works, but the output is unhierarchical, in alphabetic order, and looks like this, for example:

    Habitat: A-America, a–France, a–USA, B–Europe, b–Spain

    What I want is:

    Habitat: A-America, a–USA, B–Europe, a–France, b–Spain

    Is there a way to influence the sorting? Like, by ID?
    Thank you very much!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Haven’t tried it but you might use wp_get_post_terms (the docs aren’t quite complete)

    $taxonomy = 'habitat';
    $tax_args=array('orderby' => 't.term_id');
    $terms = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    foreach ($terms as $term) {
    echo $term->slug;
    }

    Thread Starter Jorinde

    (@jorinde)

    Michael, thank you for your quick response.
    I tried your proposal: yes, it works: it returns the slugs, ordered by ID. What I need are the names, with a link to the respective archive (sorry for not being clear about that in my first post). I have figured out that I can get the name when I change slug to name. But what about the links?
    Thanks!

    In the foreach put something like:

    echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';

    Thread Starter Jorinde

    (@jorinde)

    Fantastic! That is exactly what I was looking for.

    Now I only have two issues left:
    How can I exclude/hide empty taxonomies? In other words, with the original (non-sortable) solution that employed the_terms, I could define before and after bits—<li> <strong>Habitat:</strong> and </li>—that were displayed only when there was a value. How can I prevent such an empty label now?

    The other thing is: the hierarchy of the terms is not reflected by the URL. It is /habitat/france, instead of/habitat/europe/france. Can the URLs show the hierarchy? If there is no easy/known fix to the latter, I will dismiss the plan.

    Thank you for your patience.

    What about an if statement?

    if ($terms) {
    foreach ($terms as $term) {
    echo $term->slug;
    }
    }

    Not sure about the URL question.

    Thread Starter Jorinde

    (@jorinde)

    Michael, that worked like charm!
    I promise to dig deeper into this whole PHP thing, thank you so much for providing me with the necessary pointers.

    This is my full code now. I use this bit about 12 times in the template, for the various taxonomies. Is there a downside to that?

    <?php $taxonomy = 'habitat';
    $tax_args=array('orderby' => 't.term_id');
    $terms = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    if ($terms) {
    echo '<li> <strong>Habitat:</strong> ';
    foreach ($terms as $term) {
    echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
    }
    echo '</li>';
    } ?>

    Not real downside, but you could look into making it a function and just pass the taxonomy to the function–that way you’d only have 1 line in your template and the function in your theme’s functions.php.

    I’ll mark this resolved.

    Thread Starter Jorinde

    (@jorinde)

    Alright! Yes, my problem is solved. Thanks for your help.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘How to sort terms in custom taxonomies?’ is closed to new replies.