Highlight current category without using wp_list_categories
-
The sidebar on the website I’m working on displays a list of categories.
I need to somehow be able identify the current one and highlight it.
As I also need to display the posts filed under the current category, I think I can’t use wp_list_categories, which would make identifying the current category easy. So I’m using get_the_categories, but so far haven’t found a way to identify the current category.
It would be great to get some help. Here’s the code. I’m applying it on category.php template:
<ul> <?php //Here is where I display the categories that need to be displayed. Alright so far. $args = array('orderby' => 'id', 'order' => 'ASC', 'child_of' => '4', 'hide_empty' => 0); $categories= get_categories($args); foreach ($categories as $category) { echo '<li><a href="' . get_category_link( $category->term_id ) . '"'; // This is my last try to identify the current category and apply the class "sel" to it. That's where I think I'm failing. $category = get_category( get_query_var( 'cat' ) ); $cat_id = $category->cat_ID; if($cat_id->cat_ID == $cat->cat_ID){ echo ' class="sel"'; } // The category name echo '>' . $category->name . '</a>'; ?> // The posts under the category. This should be displayed only for current category, but it appears to all of them. <ul> <?php the_post(); foreach( ( get_the_category() ) as $category ) $my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100'); if( $my_query ) { while ( $my_query->have_posts() ) { $my_query->the_post(); ?> <li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> </li> <?php } } ?> </ul> </li> <?php } ?> </ul>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Highlight current category without using wp_list_categories’ is closed to new replies.