开发者

wp_list_categories does not show current category

开发者 https://www.devze.com 2022-12-16 02:19 出处:网络
My blog\'s main menu is made of the categories, displayed via the wp_list_categories function. If i click on one of the categories, the current category is highlighted in the category menu, and the l

My blog's main menu is made of the categories, displayed via the wp_list_categories function.

If i click on one of the categories, the current category is highlighted in the category menu, and the list of articles inside that category are listed. Everything is fine.

But if i then click on one article, the Category menu does not show the current category anym开发者_如何学Goore. Anyone knows how i could fix that?

Here is the code i use to generate the menu in the sidebar.

<?php
wp_list_categories('child_of=55&sort_column=menu_order&sort_order=asc&title_li=');
?>


I found a good hack on the wordpress forum. It will only show one "current" category but it's enough for my needs.

<?php
    if  (!is_page() && !is_home() && !is_single()){
    $catsy = get_the_category();
    $myCat = $catsy->cat_ID;
    $currentcategory = '&current_category='.$myCat;
}

    elseif (is_single()){
    $catsy = get_the_category();
    $myCat = $catsy[0]->cat_ID;
    $currentcategory = '&current_category='.$myCat;
}

    wp_list_categories('depth=1&title_li=&orderby=id&exclude=1,5,6,19,20,21,22&hide_empty=0'.$currentcategory);
    ?>

If only highlighting 1 category when you have a multi-category-per-post system, you might want to use this plugin instead (add a .used-cat class in your stylesheet, alongside the .current-cat class provided by wordpress).


'parent' isn't a valid argument for wp_list_categories.

Also, are you sure the same code is providing the category list for both the category page and the post page? The code might be within an is_category or is_post/is_page block.

0

精彩评论

暂无评论...
验证码 换一张
取 消