WordPress.org

Ready to get started?Download WordPress

Forums

wp_list_categories, style current category in attachment.php (10 posts)

  1. 2bak860
    Member
    Posted 4 years ago #

    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

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  3. 2bak860
    Member
    Posted 4 years ago #

    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

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  5. 2bak860
    Member
    Posted 4 years ago #

    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? :-)

  6. esmi
    Forum Moderator
    Posted 4 years ago #

    Not for use with wp_list_categories. :-(

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    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?...

  8. esmi
    Forum Moderator
    Posted 4 years ago #

    @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.

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    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?

  10. 2046
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.