WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Current term class when on single custom post (2 posts)

  1. simonwammerfors
    Member
    Posted 1 year ago #

    I'm using wp_list_categories in a sidebar (outside the loop) to output a tree of links of all terms of a specific taxonomy. When I click one of the links and go to the archive page of that term the list in my sidebar gets updated with a current-cat class for the current term. What I can't figure out is how to keep this class in my sidebar list as I'm visiting one of the single posts listed at the term archive page.

    I guess I somehow have to populate the parameter current_category in some dynamic way.I've found some solutions to this situation in the formus, but only when displaying actual categories in the list, not terms.

    Any suggestions on how to do this?

  2. simonwammerfors
    Member
    Posted 1 year ago #

    Solved it with this:

      <?php

      global $post;
      $taxonomy = 'produktslag';
      $term_id = 0;
      if(is_singular('produkt')){ // post type is optional.
      $terms = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "ids") );
      if(!empty($terms))
      $term_id = $terms[0]; //we need only one term id
      }

      wp_list_categories('orderby=name&taxonomy=produktslag&title_li=&current_category=' .$term_id); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic