WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom taxonomies terms as unordered list li classes (3 posts)

  1. Marco Galasso
    Member
    Posted 1 year ago #

    Hello,

    I have a custom post type with 3 custom taxonomies "alpha", "beta" and "gamma".

    I need to outuput the terms belonging to each taxonomy as classes for an unordered list.
    I mean

    <ul>
    <li class="alpha's term(s) beta's term(s) gamma's therm(s)">lorem ipsum</li>
    <li class="alpha's term(s) beta's term(s) gamma's therm(s)">lorem ipsum</li>
    <li class="alpha's term(s) beta's term(s) gamma's therm(s)">lorem ipsum</li>
    </ul>

    Is there a quick way to do it?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think that get_terms() can do what you want:

    $terms = get_terms(array('alpha','beta','gamma'));
    foreach ($terms as $term) {
       $classes .= $term->slug . ' ';
    }

    Then your li items will use $classes as the class list. It will not be in order by taxonomy, but in a list of classes this should not matter.

  3. Marco Galasso
    Member
    Posted 1 year ago #

    hello, I used wp_get_object_terms()

    <li class="all <?php
    $specialties_tax = (array('alpha','beta','gamma'));
    $specialties_terms = wp_get_object_terms($post->ID, $specialties_tax, $args);
    if(!empty($specialties_terms)){
    if(!is_wp_error( $specialties_terms )){
    foreach($specialties_terms as $term){
    echo ' '.$term->name.' ';
    }
    }
    }
    ?>">

    Now the next step is to make them filterable via a jQuery script.

Topic Closed

This topic has been closed to new replies.

About this Topic