Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter kbuntu

    (@kbuntu)

    I figured it out myself. Damn …maybe I shouldn’t work 16 hours a day.

    Hi t31os,

    Wonder if this thread is still active. I have the same problem as klarabella.
    I’ve used “akis” code but for whatever reason it show all the categories on some pages and sometimes it work as it supposed too.

    You code works great however is it possible to show all the subcategories of a parent a post is filed under instead showing all the parent categories?

    Thank you.

    After implementing the code I have posted above I’ve noticed it will just show the current “Sub Category” of the post it was posted in and not my additional subcategories under the parent. In addition it won’t show the correct sub category when you are viewing the post and instead displays all the categories.

    After searching around on the forums here is what I’ve found:

    <?php
    if (is_home()) {
    wp_list_categories('orderby=id&title_li=&depth=1');
    }
    else{
    $category = get_the_category();
    $cat_term_id = $category[0]->term_id;
    $cat_category_parent = $category[0]->category_parent;
    $listcat = wp_list_categories('echo=0&child_of='.$cat_category_parent.'&title_li=');
    $listcat = str_replace("cat-item-".$cat_term_id, "cat-item-".$cat_term_id." current-cat", $listcat);
    if ( in_category( $cat_term_id ) || post_is_in_descendant_category( $cat_term_id )) {
    echo $listcat;
    }
    }
    ?>
    </ul>

    What does the code do: It lists all the parent categories on the Homepage. If you have created several subcategories it will display them in their belonging parent category and hide all other parent and their sub categories. The post will show the correct subcategory (and other sub categories belonging to the same parent category) but will exclude all the other sub and parent.

    Hope the explanation makes sense. Paste the code in the sidebar and try it.

    In case somebody is interested. I’m not a programmer but it worked for me. I have modified the code a bit so it just shows the parent category on the homepage.

    <?php
    $categoryList = array();
    foreach((get_the_category()) as $category) // build list of categories for this post
    {
    $categoryList[] = $category->cat_ID;
    }
    ?><ul><?php
    if (is_home()) {
    wp_list_categories('orderby=id&title_li=&depth=1'); // only show the parent categories on home page
    }
    else{
    wp_list_categories("title_li=&include=" . join(",", $categoryList)); // only show categories this post is in
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)