• Resolved hungzai

    (@hungzai)


    I’m just wondering is this possible to be done….

    I want to list the child category/categories of a parent of a particular post.

    For example..

    Parent : Country

    Child : Australia, Brazil, China etc etc etc

    If a post is in China(which is a child of country), I want to be able to do something like…if it is in category country…then list the child category of it which is one of the countries.

    if ( in_category(‘category ID’)…..
    list child category/categories
    else…
    do nothing…

    Can expert share how this can be done?

    Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter hungzai

    (@hungzai)

    One big problem i am facing in this is that a post would not include the parent category and so when I use something like in_category..it returns negative….any experts know how to go around this?

    so in this example, what you want to do is to list Australia, Brazil china etc etc?

    Thread Starter hungzai

    (@hungzai)

    Yes, I want to list australia brazil or china…the children category of the parent category.

    Say if i do a post and one of the categories is Australia..i want to list it by using

    if in category country(parent)..then list australia..

    can that be done?

    The problem i am facing now is…

    1) When i query a if in category of parent, it returns negative because the post itself was not list in the parent but child.

    2) I have no idea how to list child of a parent category of a post..

    I think the following function is what you need.

    /**
     * Tests if any of a post's assigned categories are descendants of target categories
     *
     * @param mixed $cats The target categories. Integer ID or array of integer IDs
     * @param mixed $_post The post
     * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
     * @see get_term_by() You can get a category by name or slug, then pass ID to this function
     * @uses get_term_children() Gets descendants of target category
     * @uses in_category() Tests against descendant categories
     * @version 2.7
     */
    function post_is_in_descendant_category( $cats, $_post )
    {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category');
    		array_push($descendants,$cat);
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }
    Thread Starter hungzai

    (@hungzai)

    Alright thanks first. And I may need to ask you where do I put this piece of code and what do i need to put inside my single.php..

    Thanks for your great help anyway.

    you can put these codes in your functions.php which should be in your template root directory. Then you will be able to call this function in your single.php file.

    for example:

    if(post_is_in_descendant_category($your_cat_id,$your_post_id)) {
    wp_list_categories( “child_of=$your_cat_id”);
    }

    Get me informed please.

    Thread Starter hungzai

    (@hungzai)

    It worked!!! Thanks!!!

    For those who are keen, the below code works too.

    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(CAT_ID, $childcat)) {
    echo ‘cat_ID).'”>’;
    echo $childcat->cat_name . ‘
    ‘;

    simple replace the CAT_ID with the parent category ID

    Thanks for your help dude!

    You are welcome.Well done!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘List child category if in parent category’ is closed to new replies.