如何在不先加载父产品类别的情况下使用此 woocommerce 产品手风琴?

发布于 2025-01-09 20:31:51 字数 3523 浏览 1 评论 0原文

 * WooCommerce Product List as accordion (BS5)
 */
function ct_render_cat_list()
{
    global $wp_query;
    $current_cat = $wp_query->get_queried_object();
    $current_term_id = null;
    $current_parent_id = isset($current_cat->term_id) ? $current_cat->term_id : null;
    if (!is_null($current_parent_id)) {
        if ($current_cat->parent != 0) {
            $current_parent_id = $current_cat->parent;
        }
        $current_term_id = $current_cat->term_id;
    }
    /**
     * Product categories list
     */
    $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => true,
        'parent' => 0,
    );
    $product_cat = get_terms($args);

    echo '<div class="accordion" id="accordionExample">';
    foreach ($product_cat as $parent_product_cat) {
        $parent_id = $parent_product_cat->term_id;
        // skip 'Uncategorized'
        if ($parent_product_cat->term_id == '15') {
            continue;
        }
        $button = '
                  <a class="accordion-button collapsed text-decoration-none" href="' . get_term_link($parent_product_cat->term_id) . '">
                    ' . $parent_product_cat->name . ' (' . $parent_product_cat->count . ')
                  </a>
            ';
        if ($parent_id == $current_parent_id) {
            $button = '<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse' . $parent_id . '" aria-expanded="true" aria-controls="collapse' . $parent_id . '">
                       ' . $parent_product_cat->name . ' (' . $parent_product_cat->count . ')
                    </button>';
        }
        echo '
<div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
        ' . $button . '
    </h2>
          ';
        if ($parent_id == $current_parent_id) {
            $child_args = array(
                'taxonomy' => 'product_cat',
                'hide_empty' => false,
                'parent' => $parent_product_cat->term_id
            );
            $child_product_cats = get_terms($child_args);
            if (!empty($child_product_cats)) :
                echo '<div id="collapse' . $parent_id . '" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
              <div class="accordion-body"> ';
                echo "<ul class='navbar-nav me-auto mb-2 mb-md-0 '>";
                foreach ($child_product_cats as $child_product_cat) {
                    $active_class = '';
                    if ($current_term_id == $child_product_cat->term_id) {
                        $active_class = 'active';
                    }
                    echo '<li class="nav-item"><a href="' . get_term_link($child_product_cat->term_id) . '" class="nav-link link-dark ' . $active_class . '">' . $child_product_cat->name . ' (' . $child_product_cat->count . ')</a></li>';
                }
                echo '</ul>';
                echo '</div>
                </div> <!-- / accordion-collapse -->';
            endif;
        }
        echo '</div> <!-- accordion-item -->';
    }
    echo '</div> <!-- / accordion -->';
}

以下是将产品类别显示为手风琴的代码。对我来说,这里的问题是,如果我想打开手风琴,我首先必须加载 $parent_product_cat 但我想在不加载它的情况下使用手风琴并立即显示下拉列表。目前页面上的主要类别是锚标记,当加载主要类别时它们将更改为按钮。希望你能明白我的意思:)

 * WooCommerce Product List as accordion (BS5)
 */
function ct_render_cat_list()
{
    global $wp_query;
    $current_cat = $wp_query->get_queried_object();
    $current_term_id = null;
    $current_parent_id = isset($current_cat->term_id) ? $current_cat->term_id : null;
    if (!is_null($current_parent_id)) {
        if ($current_cat->parent != 0) {
            $current_parent_id = $current_cat->parent;
        }
        $current_term_id = $current_cat->term_id;
    }
    /**
     * Product categories list
     */
    $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => true,
        'parent' => 0,
    );
    $product_cat = get_terms($args);

    echo '<div class="accordion" id="accordionExample">';
    foreach ($product_cat as $parent_product_cat) {
        $parent_id = $parent_product_cat->term_id;
        // skip 'Uncategorized'
        if ($parent_product_cat->term_id == '15') {
            continue;
        }
        $button = '
                  <a class="accordion-button collapsed text-decoration-none" href="' . get_term_link($parent_product_cat->term_id) . '">
                    ' . $parent_product_cat->name . ' (' . $parent_product_cat->count . ')
                  </a>
            ';
        if ($parent_id == $current_parent_id) {
            $button = '<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse' . $parent_id . '" aria-expanded="true" aria-controls="collapse' . $parent_id . '">
                       ' . $parent_product_cat->name . ' (' . $parent_product_cat->count . ')
                    </button>';
        }
        echo '
<div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
        ' . $button . '
    </h2>
          ';
        if ($parent_id == $current_parent_id) {
            $child_args = array(
                'taxonomy' => 'product_cat',
                'hide_empty' => false,
                'parent' => $parent_product_cat->term_id
            );
            $child_product_cats = get_terms($child_args);
            if (!empty($child_product_cats)) :
                echo '<div id="collapse' . $parent_id . '" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
              <div class="accordion-body"> ';
                echo "<ul class='navbar-nav me-auto mb-2 mb-md-0 '>";
                foreach ($child_product_cats as $child_product_cat) {
                    $active_class = '';
                    if ($current_term_id == $child_product_cat->term_id) {
                        $active_class = 'active';
                    }
                    echo '<li class="nav-item"><a href="' . get_term_link($child_product_cat->term_id) . '" class="nav-link link-dark ' . $active_class . '">' . $child_product_cat->name . ' (' . $child_product_cat->count . ')</a></li>';
                }
                echo '</ul>';
                echo '</div>
                </div> <!-- / accordion-collapse -->';
            endif;
        }
        echo '</div> <!-- accordion-item -->';
    }
    echo '</div> <!-- / accordion -->';
}

Here is the code for showing product categories as accordion. The problem here for me is that if i want to open the accordion, i first have to load the $parent_product_cat but i want to use the accordion without loading it and show me dropdown right away. At the moment the main category on page is anchor tags and they will change to buttons when main category is loaded. Hope you'll get my point :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文