【问题标题】:WooCommerce shortcode products listWooCommerce 简码产品列表
【发布时间】:2017-12-29 19:29:52
【问题描述】:

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

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

短代码如下:[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' );}

但是我得到一个错误。它说 implode 函数有问题。

【问题讨论】:

  • 我说内爆函数有错误
  • 警告:implode():传递的参数无效
  • 没有警告也没有错误……请在下面的答案中查看我的 2 个经过测试的实时链接。该错误是由于您的安装中的其他原因造成的。您需要将 PHP 5.6 与 WP 和 WooCommerce 一起使用……这段代码完美运行

标签: php wordpress plugins woocommerce shortcode


【解决方案1】:

这是我在您删除的上一个问题上的原始答案,并且您在此处使用: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"]

你会得到这个:

-

【讨论】:

    【解决方案2】:

    $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);

    【讨论】:

    • 请添加一些代码格式。因为它不是很可读。
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 2021-01-30
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    相关资源
    最近更新 更多