WordPress.org

Forums

Is there any function in wordpress which can detect parent category of any post? (5 posts)

  1. vivsin
    Member
    Posted 6 years ago #

    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')

  2. churchthemer
    Member
    Posted 6 years ago #

    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.

  3. vivsin
    Member
    Posted 6 years ago #

    Yes I got " No Categories " :D

    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'); ?>
  4. churchthemer
    Member
    Posted 6 years ago #

    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");
        }
    }
    ?>
  5. vivsin
    Member
    Posted 6 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic