admin管理员组

文章数量:1316681

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/term //current-cat works perfect
www.mysite/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.

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/term //current-cat works perfect
www.mysite/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 edited Jul 31, 2012 at 9:36 mathiregister asked Jul 31, 2012 at 8:30 mathiregistermathiregister 1,54313 gold badges55 silver badges78 bronze badges 2
  • Does $terms returns any term id? Just checking, you did update the wr_list_taxonomy() function according to my code right? – Sisir Commented 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 – mathiregister Commented Jul 31, 2012 at 10:04
Add a comment  | 

2 Answers 2

Reset to default 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.

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
   )
 );
 ?>

本文标签: custom taxonomywplistcategories()currentcat class also inside posts