admin管理员组

文章数量:1122832

I am trying to get third Level WooCommerce categories. Here follows my source code:

$args = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' =>18,
    'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<div class="wooc_sclist">';
foreach ($subcats as $sc) {
    $link = get_term_link($sc->slug, $sc->taxonomy);
    echo '<ul><a href="' . $link . '">' . $sc->name . '</a>';
$args2 = get_term(array(
            'hierarchical' => 0,
            'hide_empty' => false,
           // 'taxonomy' => 'product_cat'
        ));
        $subsubCats = get_categories($agrs2);
        foreach ($subsubCats as $subsubCats) {
            $SubLinks = get_term_link($subsubCats->slug, $subsubCats-
   >taxonomy);
            echo '<li>sub -<a href=' . $SubLinks . '>' . $subsubCats->name .'</a></li>';
        }
echo '</ul>';
}
echo '</div>';
wp_reset_query();

I am trying to get third Level WooCommerce categories. Here follows my source code:

$args = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' =>18,
    'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<div class="wooc_sclist">';
foreach ($subcats as $sc) {
    $link = get_term_link($sc->slug, $sc->taxonomy);
    echo '<ul><a href="' . $link . '">' . $sc->name . '</a>';
$args2 = get_term(array(
            'hierarchical' => 0,
            'hide_empty' => false,
           // 'taxonomy' => 'product_cat'
        ));
        $subsubCats = get_categories($agrs2);
        foreach ($subsubCats as $subsubCats) {
            $SubLinks = get_term_link($subsubCats->slug, $subsubCats-
   >taxonomy);
            echo '<li>sub -<a href=' . $SubLinks . '>' . $subsubCats->name .'</a></li>';
        }
echo '</ul>';
}
echo '</div>';
wp_reset_query();
Share Improve this question edited Apr 27, 2017 at 17:31 Paul-Beyond 1658 bronze badges asked Apr 27, 2017 at 14:52 Ammad NazirAmmad Nazir 111 bronze badge 2
  • and the question is? please edit the question, and make it very clear what is that you are asking – Mark Kaplun Commented Apr 27, 2017 at 15:30
  • I was trying to get woo commerce third level categories means -Cat One -- Cat Two --- Cat Three trying to get third level categories – Ammad Nazir Commented Apr 27, 2017 at 15:34
Add a comment  | 

2 Answers 2

Reset to default 1

I Solved it by my Self

      $args = array(
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0,
        'parent' => $number = 16,
        'taxonomy' => 'product_cat'
    );
    $subcats = get_categories($args);
    echo '<div class="wooc_sclist">';
    foreach ($subcats as $sc) {
        $link = get_term_link($sc->slug, $sc->taxonomy);
        echo '<ul><a href="' . $link . '">' . $sc->name . '</a>'.$sc->term_id;
        $args2 = get_terms('product_cat',array(
            'child_of' => $sc->term_id,
            'hierarchical' => 1,
            'hide_empty' => 1,
        ));
        foreach ($args2 as $subsubCats) {
            $SubLinks = get_term_link($subsubCats->slug, $subsubCats->taxonomy);
            echo '<li>sub - <a href=' . $SubLinks . '>' . $subsubCats->name . '</a></li>';
        }
        echo '</ul>';
    }
    echo '</div>';
    wp_reset_query();

How to get disp all third-level product categories in your WooCommerce store.

// Fetch all product categories
$all_categories = get_terms( array(
    'taxonomy'   => 'product_cat',
    'hide_empty' => false,
) );

// Organize categories by their parent ID
$categories_by_parent = array();
foreach ( $all_categories as $category ) {
    $categories_by_parent[$category->parent][] = $category;
}

// Function to get third-level categories
function get_third_level_categories( $categories_by_parent, $parent_id = 0, $current_level = 1, $target_level = 3 ) {
    $third_level_categories = array();

    if ( isset( $categories_by_parent[$parent_id] ) ) {
        foreach ( $categories_by_parent[$parent_id] as $category ) {
            if ( $current_level == $target_level ) {
                $third_level_categories[] = $category;
            } else {
                $third_level_categories = array_merge(
                    $third_level_categories,
                    get_third_level_categories( $categories_by_parent, $category->term_id, $current_level + 1, $target_level )
                );
            }
        }
    }

    return $third_level_categories;
}

// Get third-level categories
$third_level_categories = get_third_level_categories( $categories_by_parent );

// Display third-level categories
foreach ( $third_level_categories as $category ) {
    echo '<li>' . $category->name . '</li>';
}

本文标签: Get Third Level Categories WooCommerce