WordPress.org

Support

Support » Themes and Templates » wp_list_categories, style current category in attachment.php

wp_list_categories, style current category in attachment.php

  • Hi,

    Does anyone know how to style wp_list_categories for the current category when in a attachment.php of a single in that category?

    thanks

    Ross

Viewing 9 replies - 1 through 9 (of 9 total)
  • esmi
    Forum Moderator

    @esmi

    Have you cracked how to highlight the current category when displaying a single post? Only I can’t see an immediate solution to that either.

    Hey Esmi,

    Does styling the current category work? I think the single post can only have one category:

    http://codex.wordpress.org/Template_Tags/wp_list_categories#Markup_and_Styling_of_Category_Lists

    thanks, Ross

    esmi
    Forum Moderator

    @esmi

    The classes .current-cat etc aren’t added when you’re viewing a single post. They only seem to apply when viewing the actual catgeory pages.

    I see what you mean. In that case, you could just use normal links (not wp_list_categories) and either use a conditional (if in_category) or take the post class, which should list the category the post is in and style that way.

    Any luck on finding a fix for my issue? 🙂

    esmi
    Forum Moderator

    @esmi

    Not for use with wp_list_categories. 🙁

    Can you give me a usage example i can test, something dead basic…

    I’m trying to understand the problem.

    wp_list_categories() supports a current category parameter, can’t you simply check the current post’s category then assign that as the current category when calling wp_list_categories ..

    Am i missing something?…

    esmi
    Forum Moderator

    @esmi

    @t31os_: When viewing a single post, that post’s category isn’t being highlighted. Not even the cat with the lowest id. wp_list_categories’ classes are only rendered when you’re viewing a category page – not a single post page.

    I see classes on the output, i’m using 2.9b2, but i can’t see any major differences between the code in 2.9 and 2.8 in category-template.php for that particular function.

    Tested a basic call to the function in the default theme, in single.php and category.php.

    The only difference i can see on output is the absence of the “current-cat” class when on single posts, the rest are still there on a single post, cat-item , cat-item-5 , etc…

    Could you not get the current post’s category ID, and pass that along as the “current_category” arg in wp_list_categories?

    2046
    Participant

    @o-o

    this is my away:

    put it in the header

    //list of allowed categories that you have actually in the menu
    $menu_cat_array = array(26,4,3262,3,13,5);
    // get names of all categories
    $categories = get_the_category();
    foreach($categories as $category){
    //get name of each category
      $cat_name = $category->cat_name;
      // get id of each category
      $cat_id = get_cat_ID($cat_name);
      // check similarities with the menu category list($menu_cat_array)
      if (in_array($cat_id,$menu_cat_array)){
        // if it fits, render this code
        echo '<style type="text/css" media="screen">.cat-item-'.$cat_id.' a{font-weight:bold;}</style>';
      }
    }

    because the post can be in many categories and you do not have to have them all listed in menu, it checks if one of the categories of the post fits to the ones you have in your menu, And if that’s true, then it creates a style for that particular menu item.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘wp_list_categories, style current category in attachment.php’ is closed to new replies.