• Resolved uili

    (@uili)


    i’m using this code to list all categories on my menu:

    <?php
    $args=array(
    'orderby' => 'name',
    'order' => 'ASC'
    );
    						$categories=get_categories($args);
    foreach($categories as $category) {
    echo '<li><a href="'. get_bloginfo('url').'#' . $category->category_nicename . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';}
    ?>

    how to add class when i’m on the single page of the current category?

Viewing 2 replies - 1 through 2 (of 2 total)
  • http://codex.wordpress.org/Function_Reference/wp_get_post_categories

    <?php if( is_single() ) $post_cats = wp_get_post_categories($post->ID);
    $args=array(
    'orderby' => 'name',
    'order' => 'ASC'
    );
    						$categories=get_categories($args);
    foreach($categories as $category) {
    echo '<li'.((is_single() && in_array( $category->term_id, $post_cats) )?' class="current-cat"':'').'><a href="'. get_bloginfo('url').'#' . $category->category_nicename . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';}
    ?>

    that puts a css class of .current-cat on the li element.

    Thread Starter uili

    (@uili)

    ahhh nice! thats the code i couldnt find out!

    thanks a lot, gonna study this!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add class for current category page’ is closed to new replies.