Support » Fixing WordPress » highlight category links in sidebar once again

  • Hello,
    I managed to display categories in sidebar with descriptions. Now I want to highlight current category name when user is on category archive or displays the post from given category. I maganed to add class to the category when user is on category archive but when he displays any posts only the first category is highlighted. My code is:

    <?php $argss = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
         'exclude'                  => 1,
        'taxonomy'                 => 'category',
        'pad_counts'               => false );
    ?>
    <div class="sidemenu">
    
    <?php
    
    $categories=get_categories($argss);
      foreach($categories as $category) {
    		echo '<p class=""><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . 'class="';
    	if(is_category || is_single()){
    		$cat = get_category_by_path(get_query_var('category_name'),false);
    		$current = $cat->cat_ID;
    		$current_name = $cat->cat_name;
    
    		if ($current == $category->cat_ID) {
    			echo 'current-category';
    		}
    		else {
    			echo $category->cat_ID;
    		}
    
        }
    		echo '">' . $category->name .  '</a>'  . '<br />' . '<span class="mendesc">' . $category->description . '</span>' . '<span style="font-size: 10px; float: right">' . ' (' . $category->count . ')</span>' . '</p>';
    }
    
    ?>
    </div>

    Thanks in advance for reply!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter areck

    (@areck)

    anyone? :)still not resolved. It must be very similar to what i’ve done to highlight category when user is in category archive.

    Thread Starter areck

    (@areck)

    Ok I’ve changed the code and I managed to display categories in sidebar and I can highlight them when entering single post or category archive. But a strange thing happened. When I moved my site to another hosting provider I’m not able to highlight category when I’m in category archive! (Code is is still the same!)

    Here’s a code:

    <div class="sidemenu">
    <?php
    
    	  $categories = get_categories('depth=1&hide_empty=false&exclude=1');
    
            foreach ($categories as $cat) {
    
             	 // Gets currently selected category id.
    			 $id = get_the_ID();
    		  	 $category = get_the_category($id);
    
    			 if($category[0]->cat_ID == $cat->cat_ID){
    			   if(!is_home() && !is_author() && !is_tag() &&){ $link_color = 'style="color:#336699;"'; }
    
    				echo '<a href="'.get_option('home').get_option('category_base').'/category/'.$cat->category_nicename.'/"' . $link_color . '>'.$cat->cat_name.'</a> ';
    			   }
    			 else{
    			   echo '<a href="'.get_option('home').get_option('category_base').'/category/'.$cat->category_nicename.'/">'.$cat->cat_name.'</a>';
    			}
    
                if ($cat->category_description != '') {
                    echo '<p><span class="mendesc">' .$cat->category_description. '</span><span style="font-size: 10px; float: right">' . ' (' . $cat->category_count  . ')</span></p>';
                }
    			else{
    	            echo '<p>&nbsp;</p>';
    			}
                if ($cat->category_parent != 0) {
                    echo '<p>&nbsp;</p>';
    
                }
            }
            ?>
    </div>

    this is so strange! is the hosting provider server not supporting some wp variables?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘highlight category links in sidebar once again’ is closed to new replies.