编写 woocommerce 自定义短代码

时间:2021-02-03 17:13:07

标签: woocommerce shortcode woocommerce-theming

嗨,我已经编写了以下自定义 woocommerce 短代码 .. 但它不太正确。在调用短代码之前,我在页面顶部获得输出。我认为编码结构/包装函数需要一些工作吗?非常感谢任何帮助!

我还需要一个 add_action( 'woocommerce_loaded', 'my_function' ) 或函数来检查 woocommerce 是否处于活动状态?如果是这样,我会把这些放在哪里?


add_shortcode( 'boxes', 'boxes_loop' );

function boxes_loop() {
    $query_args = array(
        'featured' => true,  
        'category' => array( 'veg-box' ),
    );
    $products = wc_get_products( $query_args );
    
global $post;
    
    $result = '<div>';
    
    woocommerce_product_loop_start();


    foreach ($products as $product) {
    
        $post = get_post($product->get_id());
        setup_postdata($post);

        $result .= get_the_title();
  
    }
    wp_reset_postdata(); woocommerce_product_loop_end();

    $result .= '</div>';

    return $result;

}

0 个答案:

没有答案
相关问题