• Resolved rodrego

    (@rodrego)


    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)
  • Moderator bcworkz

    (@bcworkz)

    You have the right idea, but you’re going in circles and tripping on yourself ?? (I’m laughing because I’ve done things similar and worse, no offense intended)

    You correctly got the current category ID from the query var ‘cat’, but then you overwrote the loop $category with the current $category, just to get the ID which you had all along. You then compare it with $cat which I’ve no idea where it came from. All you need to do to identify the current category is if($category->cat_ID == get_query_var( 'cat' ))

    This only works when the current query is for a category query, which I assume is the case on a category.php template.

    Thread Starter rodrego

    (@rodrego)

    Thanks bcworkz, it works like charm!

    No offense taken! I’m no developper, so when the code gets messy I have little idea what I’m actually doing. I try to adapt working solutions of similar situations I find on the internet, and sometimes I fail miserably.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Highlight current category without using wp_list_categories’ is closed to new replies.