WordPress.org

Support

Support » How-To and Troubleshooting » function get_category_children has a line i can’t find its purpose;

function get_category_children has a line i can’t find its purpose;

  • Hi guys, i am modifying my template and i read this function of wordpress but there is a line that if erased nothing changes so i guess it does nothing.The line is emphasized below.

    function get_category_children($id, $before = '/', $after = '') {
    if ( 0 == $id )
    return '';
    $cat_ids = get_all_category_ids();
    foreach ( $cat_ids as $cat_id ) {
    if ( $cat_id == $id)
    continue;
    $category = get_category($cat_id);
    if ( $category->category_parent == $id ) {
    $chain .= $before.$category->cat_ID.$after;
    $chain .= get_category_children($category->cat_ID, $before, $after);
    }
    }
    return $chain;
    }

    This is the issued line:
    $chain .= get_category_children($category->cat_ID, $before, $after);

    What’s it purpose in the above code and why if erased the function still returns the children of the category transmitted as parameter?
    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • That line makes it recursive. Without it, it’s not recursive.

    If you have categories like this:
    Parent
    -Child
    --GrandChild

    …then calling it on the Parent gives you both the Child and the GrandChild. Without that line, it’ll just give you the Child.

    And note that I would really recommend AGAINST editing code when you don’t understand what it does. Erasing lines randomly to see what happens is a poor way to figure things out, and very likely to break things. Badly.

    🙂 i’m not erasing lines i just didn’t get it what it does so i asked. Actually i’m not modifying anything wordpress comes as default with. It was just a thing i did to see what happends because i didn’t understand its purpose. I don’t delete things for good, but thanks for making things clear.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘function get_category_children has a line i can’t find its purpose;’ is closed to new replies.
Skip to toolbar