• Resolved nicksoper

    (@nicksoper)


    Hi There,

    I just found this great plugin to see if a page is a child of another page (https://www.red-root.com/wordpress/wordpress-plugin-is-child-of/).

    I was wondering if anyone knows if there is a plugin or a bit of PHP that could find out if the page you are on is a child of category (id)?

    At the moment is_category(id) and in_category(id) and up having too much overlap for the theme I am coding, and I just need to check if the category I am in is a child of a top level category

Viewing 4 replies - 1 through 4 (of 4 total)
  • From my notes, this might work for you:

    <?php
    // in category template, test if queried category is child descendant of another category
    if ( is_category() ) {
    $parent = 8;
    $categories = get_categories('include='.get_query_var('cat'));
    //echo "<pre>"; print_r($categories); echo "</pre>";
    if ( $categories[0]->category_parent == $parent ) {
    echo 'category ' . $categories[0]->name . ' is a child of category ' . $parent;
    }
    }
    ?>

    Of course if t31os_ comes along he’ll nudge me that get_terms is faster than get_categories…

    Thread Starter nicksoper

    (@nicksoper)

    Hi Michael,

    Thanks for the snippet. My internet was being slow (due to a national network issue here in South Africa) and you actually replied BEFORE my page reloaded. ??

    I managed to hack this bit of code that works, but (it is still early here so my brain is fuzzy), I was wondering how I can make it a bit more flexible for other categories.

    <?php
    // in category template, test if queried category is child descendant of another category
    
    if (is_category()) {
    
    $parent = 13; //it would be great if this was a variable of the current parent category
    
    $categories = get_categories('include='.get_query_var('cat'));
    
    if ( $categories[0]->category_parent == $parent ||  is_category($parent)) {
    
    wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=0&child_of='. $parent);
    
    }
    }
    ?>

    At the moment it works if you are in Category 13 or one of it’s child, but I have several other categories that need the same functionality. eg. How could I make this snippet work if are in say category 20 or one of its childs, and do the same thing?

    Perhaps you could get_parent_category, then parse that into the $parent variable?

    Would it be possible to assist with another amazing code snippet? Sorry if this seems trivial.

    Thread Starter nicksoper

    (@nicksoper)

    I found this code that gets the parent category ID, any tips on how to alter it to get the TOP level parent category?

    <?php
    $category = get_the_category();
    $parent = get_cat_name($category[0]->category_parent); //make this line go to the top level category?
    if (!empty($parent)) {
    echo '&raquo; ' . $parent;
    } else {
    echo '&raquo; ' . $category[0]->cat_name;
    }
    ?>

    Thread Starter nicksoper

    (@nicksoper)

    Resolved. Here is the solution for is_child_of() or is_child_of_category

    After looking through various posts and examples I found this forum thread in the wordpress support forum, and someone linked to the documentation around this topic which is in the codex documentation section here.

    As for my solution, this is what I ended up with:

    <?php
    if (in_category(10) || post_is_in_descendant_category(10)) {
    wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=0&child_of=10');
    }
    ?>

    So I will have to add each a conditional statement for each parent (ancestor) category, but all the child/sub categories will automatically populate the list with links of the subcategories in the parent category, which is more important than the top level categories changing that much.

    Note: for post_is_in_descendant_category() to work you will need to add the function to your functions.php file or include is. You can copy paste that code from the codex documentation on this page https://codex.www.ads-software.com/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘is_child_of_category’ is closed to new replies.