WooCommerce短代码产品列表

问题描述 投票:0回答:2

我必须制作一个Wordpress插件,为WooCommerce添加短代码。我想从特定产品类别中获取产品以及要显示的最大产品数量。短代码参数应该是类别ID和产品限制。我想我应该使用WP_Query对象。

我需要让它看起来像这样:

enter image description here

短代码将是这样的:[productslist_category="[category_ID]" limit="[product_limit]"]

我使用下面的代码from this answer(感谢LoicTheAztec):

if( !function_exists('products_list_in_a_product_category') ) {

function products_list_in_a_product_category( $atts ) {

    // Shortcode Attributes
    $atts = shortcode_atts(
        array(
            'cat'       => '',
            'limit'     => '4', // default product per page
            'column'    => '4', // default columns
        ),
        $atts, 'productslist'
    );

    // The query
    $posts = get_posts( array(
        'post_type'      => 'product',
        'posts_per_page' => intval($atts['limit'])+1,
        'product_cat'    => $atts['cat'],
    ) );

    $output = '<div class="products-in-'.$atts['cat'].'">';

    // The loop
    foreach($posts as $post_obj)
        $ids_array[] = $post_obj->ID;

    $ids = implode( ',', $ids_array );

    $columns = $atts['column'];

    $output .= do_shortcode ( "[products ids=$ids columns=$columns ]" ) . '</div>';

    return $output;
}
add_shortcode( 'productslist', 'products_list_in_a_product_category' );}

但是我收到了一个错误。它说内爆功能有问题。

php wordpress plugins woocommerce shortcode
2个回答
2
投票

这是我原来的答案,这是你删除的上一个问题,以及你在这里使用的地方:Display WooCommerce products with a custom shortcode based on a category

该代码在woocommerce版本2.6.x和3+中完美运行。


这是我原来的答案代码(在删除之前的问题之前):

这是一个基于您的短代码和现有的[product] WooCommerce短代码的解决方案。正如您将看到的那样,您将得到您所期待的......

这是代码:

if( !function_exists('products_list_in_a_product_category') ) {

    function products_list_in_a_product_category( $atts ) {

        // Shortcode Attributes
        $atts = shortcode_atts(
            array(
                'cat'       => '',
                'limit'     => '5', // default product per page
                'column'    => '4', // default columns
            ),
            $atts, 'productslist'
        );

        // The query
        $posts = get_posts( array(
            'post_type'      => 'product',
            'posts_per_page' => intval($atts['limit'])+1,
            'product_cat'    => $atts['cat'],
        ) );

        $output = '<div class="products-in-'.$atts['cat'].'">';

        // The loop
        foreach($posts as $post_obj)
            $ids_array[] = $post_obj->ID;

        $ids = implode( ',', $ids_array );

        $columns = $atts['column'];

        $output .= do_shortcode ( "[products ids=$ids columns=$columns ]" ) . '</div>';

        return $output;
    }
    add_shortcode( 'productslist', 'products_list_in_a_product_category' );
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码在WooCommerce 3+上进行测试并且有效。


用法(示例):

[productslist cat="clothing" limit="4"]

你会得到这个:

enter image description here

-


-1
投票

$ args = array('post_type'=>'product','post_status'=>'publish','ignore_sticky_posts'=> 1,'posts_per_page'=>'12','meta_query'=> array(array('key '=>'_visibility','value'=> array('catalog','visible'),'compare'=>'IN')),'tax_query'=> array(array('taxonomy'=>'product_cat ','field'=>'term_id',//这是可选的,因为它默认为'term_id''terms'=> 26,'operator'=>'IN'//可能的值为'IN','NOT IN','AND'。))); $ products = new WP_Query($ args); var_dump($ products);

© www.soinside.com 2019 - 2024. All rights reserved.