Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Does $terms returns any term id? Just checking, you did update the wr_list_taxonomy() function according to my code right? –  Sisir Jul 31 '12 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 –  mathiregister Jul 31 '12 at 10:04

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  mathiregister Jul 31 '12 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 –  mathiregister Jul 31 '12 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. –  mathiregister Jul 31 '12 at 10:12
    
use a if statement with is_singular() conditional. Updated my answer. –  Sisir Jul 31 '12 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? –  mathiregister Jul 31 '12 at 10:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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