Support » Fixing WordPress » Is there any function in wordpress which can detect parent category of any post?

  • I was wondering if there is any function available which can detect parent category of any post?

    What I am trying to do is, In my sidebar I want to include only the child categories of the Parent to which current post belongs.

    something like this

    wp_list_cats(‘child_of=detect parent’)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Something like this should work:

    <?php
    if(empty($wp_query->post->post_parent)){
        $parent = $wp_query->post->ID;
    }
    else{
        $parent = $wp_query->post->post_parent;
    }
    wp_list_categories("child_of=$parent");
    ?>

    Hope that helps, there’s about a billion options you can specify for that function, so let me know if you need more help. You may also run into an issue with it displaying “No categories”, let me know if you need to know how to get rid of that.

    Yes I got ” No Categories ” 😀

    as far as my luck is concerned ” EXPECT THE WORST “

    Also, What will happen if the child has two parents? I will make sure that this never happen but still, If i want to create a theme for public distribution, I must think about this.

    If you may like to know:

    In my test blog, I have created:
    – 3 parent categories
    – 3 Child for each
    – no post under UNCATEGORIZED

    and here is what i tried to do after your reply

    Code from my Sidebar.php

    <h2>Categories</h2>

      <?php
      if(empty($wp_query->post->post_parent)){
      $parent = $wp_query->post->ID;
      }
      else{
      $parent = $wp_query->post->post_parent;
      }
      wp_list_categories(‘show_count=0&hierarchical=0&title_li=&child_of=$parent’); ?>

    Ok, new approach. The code below will get the category parent id for all categories the current post belongs to. It will then display all children categories of those parents without displaying the parents themselves. This takes care of an issue where a post belonging to a subcategory and a parent would return all categories. The code checks to see if the category has parents, if it doesn’t it’s not displayed.

    <?php
    $categories = get_the_category();
    foreach($categories as $category){
        $parent = $category->parent;
        if($category->parent == 0){
        }
        else{
            wp_list_categories("child_of=$parent&title_li");
        }
    }
    ?>

    This one is working.

    Thank you for your time.

    But I think I have to use WP-Hive plugin for my magazine site. I am going to deal with around 50-70 categories 🙁

    I have no idea how am I going to create whole solution with a GOOD navigation system.

    One idea is to do something like autoblog.com, Ajax based roll down menu for subcategories.

    B/W may I know something about your profession if you don’t mind.
    u can contact me prodemon@gmail.com

    or add to facebook with same email id.

    Thanks Again
    Viv

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is there any function in wordpress which can detect parent category of any post?’ is closed to new replies.