WordPress.org

Ready to get started?Download WordPress

Forums

Give List Item the ID of the Taxonomy Name (2 posts)

  1. inhouse
    Member
    Posted 1 year ago #

    Hi, I'm using get_the_term and trying to give each list item an ID of the taxonomy name or taxonomy ID so I can target each li individually with CSS. The problem is I don't know how to escape certain characters so nothing I try is registering as PHP. Thanks in advance for any help.

    echo get_the_term_list($post->ID, 'example', '<li id=" id ">', '</li><li id="">', '</li>' );

    Output would be something like:
    <li id="taxonomy-name"><a href="#" rel="tag">Taxonomy Name</a></li>

  2. inhouse
    Member
    Posted 1 year ago #

    A plugin developer gave me a hand with this and this is the code I'm using in case it helps anyone else.

    $id = $post->ID;
    $tax = 'mcm_category_destination';
    $op = '';
    $list = get_the_term_list($id,$tax,'','|','');
    if ($list) {
       $terms = explode('|',$list);
       $op = '<ul>';
       foreach ($terms as $term) {
          $class = sanitize_title(strip_tags($term));
          $item = "<li class='$class'>$term</li>";
          $op .= $item;
       }
       $op .= '</ul>';
    }
    echo $op;

Topic Closed

This topic has been closed to new replies.

About this Topic