1

I wrote my own function to list all taxonomy terms of a certain taxonomy …

function wr_list_taxonomy($taxonomy, $orderby, $hierarchical) {
    $show_count   = 0;
    $pad_counts   = 0;
    $title        = '';

    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );

    return wp_list_categories( $args );
}

So this function works exactly like wp_list_categories() and also puts out a "current-cat" class once I'm on a term-page.

So imagine my URL structure …

www.mysite.com/term //current-cat works perfect
www.mysite.com/term/a-post-associated-with-this-term //current-cat not assigned

Is there a chance to also make the "current-cat" class work once I'm in a post but within this "category" just like pointed out above?

update:

The wr_list_taxonomy() function is called inside my header.php file.

<nav id="main-nav">
            <ul class="wr-nav" role="navigation">
                    <?php
                        global $post;
                        $taxonomy = 'event_type';
                        $term_id = 0;
                        $terms = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "ids") );
                        if ( !empty($terms) )
                            $term_id = $terms[0];
                        wr_list_taxonomy($taxonomy, 'name', 1, $term_id); ?>
            </ul>
</nav>

I updated the wr_list_taxonomy() function to your version.

Moreover I have another function that might be relevant for the thing I want …

/**
 * Add category-slug as classname to wp_list_categories()
 */

add_filter('wp_list_categories', 'add_slug_css_list_categories', 10, 2);

function add_slug_css_list_categories($list, $args) {

    if ( $args["taxonomy"] == "event_type" ) {
        $cats = get_terms('event_type');
        $class = 'term term-';
    } else {
        $cats = get_categories();
        $class = 'category-';
    }

    foreach( $cats as $cat ) {
        $find = 'cat-item-' . $cat->term_id . '"';
        $replace = $class . $cat->slug . '"';
        $list = str_replace( $find, $replace, $list );
        $find = 'cat-item-' . $cat->term_id . ' ';
        $replace = $class . $cat->slug . ' ';
        $list = str_replace( $find, $replace, $list );
    }

    return $list;
}

This function adds the "category-slug" to each <li> item in the wr_list_taxonomy() function. This works fine.

I just need to have the "current-cat" class also applied when I'm on a single.php (post) site that is associated with the "current category" i'm in.

2
  • Does $terms returns any term id? Just checking, you did update the wr_list_taxonomy() function according to my code right?
    – Sisir
    Jul 31, 2012 at 9:42
  • Yes it does! $term_id holds the ID of the term - also inside a post. So that seems to work, but the class is not applied Jul 31, 2012 at 10:04

2 Answers 2

1

Yes off course. You just need to get the term id and put it on the args. Check wp_list_categories()

function wr_list_taxonomy($taxonomy, $orderby, $hierarchical, $cat_id) {
    $show_count   = 0;
    $pad_counts   = 0;
    $title        = '';
    $cat_id = 0;

    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title,
      'current_category' => $cat_id
    );

    return wp_list_categories( $args );
}

You have to pass the category id or term id. To make it work.

How Do You Get Category ID?

For Example:

    global $post;
    $taxonomy = 'my-tax';
    $term_id = 0;
    if(is_singular('post')){ // 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
    }

   wr_list_taxonomy($taxonomy, $orderby, $hierarchical, $term_id);

Its just a simple example and there are other ways to do it. It depends on where you are using the code.

6
  • Thank you, but this doesn't make a difference. The "navigation" that lists my taxonomy-terms doesn't have "current-cat" applied once i'm inside a post. On the term-page itself the "current-cat" class is assigned to the <li>s. I updated my question with a few more details. Jul 31, 2012 at 9:33
  • Well, weird! now it does work after I changed the variable $cat_id inside of the wr_list_taxonomy() to term_id as well. 'current_category' => $term_id Jul 31, 2012 at 10:09
  • One last problem though … Trying to get property of non-object in /Users/my/htdocs/wr/wp-content/themes/wr/header.php on line 91 this happens if no post is assigned to a term on a term-page. Jul 31, 2012 at 10:12
  • use a if statement with is_singular() conditional. Updated my answer.
    – Sisir
    Jul 31, 2012 at 10:17
  • thank you! And the last question … is there a way to make this get term id more "functional" … I don't want all this php in my header.php. Is it possible to write a function get_term_id() that does all that? Jul 31, 2012 at 10:22
0

Based on the accepted answer, You just need to pass an extra parameter in. I just write down complete code so it's easy for understanding.

    <?php  $taxonomy = 'category';
 
 $term_id = 0;
    if(is_singular('post')){ // 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
    }

 echo wp_list_categories(
   array(
     'show_count'   => false,
   'pad_counts'   => false,
   'use_desc_for_title' => false,
   'title_li'=>'',
   'hierarchical' =>false,
   'current_category' => $term_id
   )
 );
 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.