Forums

Highlight menu category when on article? (3 posts)

  1. cchevy365
    Member
    Posted 2 years ago #

    Hello,

    I would like to highlight a menu category when an article from that cat. is being viewed.

    Here is the code I could not tweak:

    <li<?php if ( is_home() ) { echo " class=\"current-cat\"";}?>><a href="<?php bloginfo('url'); ?>/"><?php _e('Latest Ads','cp'); ?></a></li>
    			<?php wp_list_categories('show_count=0&hide_empty=true&orderby=name&order=asc&title_li='); ?>
    			<li><div class="clear"></div></li>

    Can somebody help me with this?

  2. alchymyth
    The Sweeper
    Posted 2 years ago #

    make a backup copy of your file before editing

    it might work, if you replace this line of your code:
    wp_list_categories('show_count=0&hide_empty=true&orderby=name&order=asc&title_li=');

    with the following:

    if(is_single()) :
    $categories = wp_get_post_categories($post->ID);
    foreach ($categories as $catid) {
    $cat = get_category($catid);
    $cats[] = $cat->cat_ID;
     }
    $cats_list =  wp_list_categories('echo=0&show_count=0&hide_empty=true&orderby=name&order=asc&title_li=');
    foreach($cats as $value) {
    	if(preg_match('#item-' . $value . '">#', $cats_list)) {
    	$cats_list = str_replace('item-' . $value . '">', 'item-' . $value . ' current-cat">', $cats_list);
    	}
    	}
    echo $cats_list;
    else :
    //use default category list
    wp_list_categories('show_count=0&hide_empty=true&orderby=name&order=asc&title_li=');
    endif;
  3. cchevy365
    Member
    Posted 2 years ago #

    Man, if you ever come to Macedonia send me an email. I'm buying the beer!

    Thanks, it works perfectly!!!

Topic Closed

This topic has been closed to new replies.

About this Topic