首页 > 解决方案 > PHP函数按需

问题描述

我在 woocommerce 中有一个自定义选项卡,可以将备件循环到当前产品。在某些情况下,备件很多,产品需要很长时间才能加载。是否有一些聪明的方法可以“按需”加载标签内容,而不是每次显示产品时?选项卡内容是一个 wp-query 循环。

这是代码的一部分:

<?php
foreach( $categories as $category ):
    $categoryID = $category->term_id;
    $categoryName = $category->name;

    //Query

    // $argsParts = array(
    //     'post_type' => 'product',
    //     'tax_query' => array(
    //         array(
    //             'taxonomy' => 'product_cat',
    //             'terms' => $categoryID,
    //             'operator' => 'IN',
    //         )
    //     ),
    //     'meta_query' => array(
    //         array(
    //             'key' => '_crosssell_ids',
    //             'value' => $current_post_id,
    //             'compare' => 'LIKE'
    //         )
    //     )
    // );

    $argsParts = array(
        'post_type' => 'product',
        'no_found_rows' => true,
        'posts_per_page' => -1,
        'post__in' => $crosssells,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'terms' => $categoryID,
                'operator' => 'IN',
            )
        ),
    );

    $queryParts = new WP_Query($argsParts);

    if($queryParts->have_posts()){
    ?>
        <li class="g-acc-list__nav__button" id="g-acc-list__nav--<?php echo $categoryID ?>"><?php echo $categoryName ?></li>
    <?php
    }

endforeach;

标签: phpwoocommercewordpress-themingwoocommerce-theming

解决方案


推荐阅读