1

I am building a FAQ section in our mobile site which I would like to have the following hierarchy:

Category > Sub-category > Sub-sub-category > Article

I would like the category list and each sub-category list to display on its own separate page, rather than use an accordion-style UI (which you find in most FAQ plug-ins).

I'm told by my developer that we can only display Category > Sub-category on separate pages in WP, but not Sub-sub-category.

I would like to know if it's possible to do what I've described in WP so that I can ask my developer to do some more research. If you are also able to explain how we should go about this, that would be a great help! 🙏

1 Answer 1

1

If I understand correctly, it certainly is possible.

I would start with the function that determines if current category has parent or not.

function category_has_parent($catid){
    $category = get_category($catid);
    if ($category->category_parent > 0){
        return $category->category_parent;
    }
    return false;
}

In the PHP template that displays your FAQ I would check if current category has parent (if function above returns it's ID) or not (returns false). If not, I would display only top level categories, by defining 'parent' => 0 in query arguments (and of course taxonomy, and other details).

$args = array(
  'taxonomy' => 'category',
  'parent' => '0'
);
$categories = get_categories($args);

You can display these categories anyway you like.

If current category has a parent, it means it is either sub-category, or sub-sub-category (or N-times "sub", doesn't matter). In that case I would want to get all children of the parent category.

function get_parent_children( $term_id = 0, $taxonomy = 'category' ) {
    $children = get_categories( array( 
        'child_of'      => $term_id,
        'taxonomy'      => $taxonomy,
        'fields'        => 'ids',
    ) );
    return ( $children );
}

Lastly, you would call the function above with your parent ID and taxonomy. If it returns it's children, you should display them anyway you like. If not, it means it only contains articles - in that case go ahead and display them.

My tip would be to use AJAX to reload the PHP generated content dynamically. You can also store the current category ID somewhere in the DOM (class or some data attribute). That way you wouldn't even have to check for parent category ID. You would pass it with AJAX as a $_GET variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.