4

Im not sure the best way to proceed here.

I have categories laid out like this (a simplified version):

-fruit
--apple
---large
---small

--banana
---var1
----large
----small
---var2
----small
----large

I want to use the depth of a category as the condition in an if statement to achieve something like this:

if category is depth 2 (apple>large) do this

else if the category is depth 3 (banana>var1>small) do something else.

Ive tried using the function from here but have been unable to get anything back other than an empty array! http://www.devdevote.com/cms/wordpress-hacks/get_depth.html

blackhill24
  • 422
  • 14
  • 30

2 Answers2

4

You could use get_ancestors() and then count() on the items returned. From there is will be simple logic.

As a function this would look like:

function so50409656_count_ancestors( $object_id, $object_type ='', $resource_type='' ) {

    $terms = get_ancestors( $object_id, $object_type, $resource_type );

    return count($terms) + 1;

}

And then called like this:

$term_depth = so50409656_count_ancestors( $term->term_id, 'your_term_type', 'taxonomy' );

if ( $term_depth === 1 ) :
    // ..your top level ancestor action
elseif ( $term_depth === 2 ) :
    // ..your direct child choice of actions here
elseif ( $term_depth === 3 ) :
    // ..your second level child action here
endif;

EDIT:

Clarified that a result of 1 will be the top level ancestor as per the OP request for something like large (in apple>large) to be equal to 2

DevLime
  • 937
  • 2
  • 9
  • 19
  • I think you need `count($terms) + 1`? – Reigel Gallarde May 21 '18 at 12:10
  • 1
    @Reigel - You certainly can but it seems arbitrary. Top level ancestors will just return 0 instead of 1 in my example. As long as you are consistent with the definition of the what the top level ancestor will return, it shouldn't really matter. – DevLime May 21 '18 at 12:20
  • Ok, I have not tested the code, but maybe you're right. What got me into conclusion though is `banana>var1>small` will return 2? and OP is expecting it as dept 3? or maybe I'm just confused with the variable `$term_depth`. But I got your point though.. Cheers! – Reigel Gallarde May 22 '18 at 01:28
  • 1
    I'm under the impression that 'fruit' is a parent term but if the OP clarifies I can change it :) – DevLime May 22 '18 at 05:04
  • sorry for the delayed reply! @Reigel is correct i wanted banana>var1>small to be a value or depth of 3 – blackhill24 May 29 '18 at 08:41
  • I've just modified my answer to reflect `fruit` as the taxonomy, not as a term in the taxonomy. :) – DevLime May 29 '18 at 08:47
0

You can use below recursive function

function hierarchical_product_cat($category = 0)
{
    $result = '';
    $args = array('parent' => $category);
    $next = get_terms('product_cat', $args);

    if ($next) {
        $result .= '<ul>';
        foreach ($next as $cat) {
            $result .= '<li><a href="' . get_term_link($cat->slug, $cat->taxonomy) . '" title="'.$cat->name.'" ' . '>' . $cat->name . ' (' . $cat->count . ')' . '</a>';
            $result .= $cat->term_id !== 0 ? hierarchical_product_cat($cat->term_id) : null;
        }
        $result .= '</li>';
        $result .= '</ul>';
    }
    return $result;
}
echo hierarchical_product_cat();
Jaydp
  • 1,029
  • 9
  • 17