• Resolved all_good_things

    (@all_good_things)


    Hi!

    I’m currently developing a WordPress theme with a nav bar on the top of the site. In the nav bar I have used <?php wp_list_categories(); ?> . I have also used the current-cat CSS tag to say that if you select a category the link will be highlighted. This works just fine.

    But I have a question:

    If you enter a single entry from the front page, how can I make the category which the entry belongs to highlight?

    Thanks for reading!
    I hope someone has a nice solution. ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • you probably have something like this for your cat nav:

    <ul>
    <?php wp_list_categories('title_li='); ?>
    </ul>

    you could try and replace it with this snippet:

    <?php
    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&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 '<ul>'.$cats_list.'</ul>';
    
    else :
    //use default category list
    echo '<ul>';
    wp_list_categories('title_li=&');
    echo '</ul>';
    
    endif;
    ?>

    as a single post can have more than one category, this will highlight all cats that are in the post.

    hope this helps and works ??

    Thread Starter all_good_things

    (@all_good_things)

    Wow! It worked brilliantly! ?? Very nice. Thank you so much!

    thanks helped me too ^^

    changed this line to list childs categories

    $cats_list = wp_list_categories( 'echo=0&child_of=3&title_li=');

    Hi,
    I have this already coded and I have tried to implement your solution above but i keep messing up the page layout.

    Here’s my code:

    <div id="nav">
    			<ul>
    
    			<li<?php
                    if (is_home())
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>">Home</a>
            </li>
    
    			<?php wp_list_categories('title_li'); ?>
    			<?php wp_list_pages('title_li' ); ?>
    			</ul>
    		</div>

    Any help would be appreciated thanks.

    @alchymyth:
    you made me very happy today! i tried a lot past days to achieve the cat highlighting when inside a post, nothing worked. your code did, instant satisfaction!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Current Category at Single entries’ is closed to new replies.