Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce中的单个帖子上获取产品类别的完整产品单个页面_Php_Wordpress_Woocommerce_Shortcode_Product - Fatal编程技术网

Php 在WooCommerce中的单个帖子上获取产品类别的完整产品单个页面

Php 在WooCommerce中的单个帖子上获取产品类别的完整产品单个页面,php,wordpress,woocommerce,shortcode,product,Php,Wordpress,Woocommerce,Shortcode,Product,我正在寻找一个代码,以显示完整的产品信息(标题的产品,购买按钮,图片库,说明等)的所有产品的一个特定的产品类别,我的选择在一个单一的职位 实际上,这并没有任何短代码,我也没有自己写东西的技能 也许有人对此有一个解决方案?这里有一个自定义短代码,它在循环中使用现有的woocommerce短代码,以输出定义产品类别的所有单个产品页面(一个接一个) 此自定义短代码有2个参数: cat用于产品类别(slug) 限制要显示的帖子数量(默认为全部) 守则: if( !function_exists('s

我正在寻找一个代码,以显示完整的产品信息(标题的产品,购买按钮,图片库,说明等)的所有产品的一个特定的产品类别,我的选择在一个单一的职位

实际上,这并没有任何短代码,我也没有自己写东西的技能


也许有人对此有一个解决方案?

这里有一个自定义短代码,它在循环中使用现有的woocommerce短代码,以输出定义产品类别的所有单个产品页面(一个接一个)

此自定义短代码有2个参数:

  • cat
    用于产品类别(slug)
  • 限制要显示的帖子数量(默认为全部)
守则:

if( !function_exists('single_product_pages_by_category') ) {

    // Add Shortcode
    function single_product_pages_by_category( $atts ) {

        // Attributes
        $atts = shortcode_atts(
            array(
                'cat' => '',
                'limit'   =>'-1'
            ),
            $atts, 'product_pages_cat'
        );

        $posts = get_posts( array(
            'post_type'      => 'product',
            'post_status'    => 'publish',
            'posts_per_page' => $atts['limit'],
            'tax_query'      => array( array(
                'taxonomy'   => 'product_cat',
                'field'      => 'slug',
                'terms'      => $atts['cat'],
            ) )
        ) );

        $output = '<div class="products-in-'.$atts['cat'].'">'; // DIV container html tag (opening)

        // Iterating though each product in the defined category
        foreach( $posts as $post ){
            $post_id = $post->ID; // The Product ID
            // Using WooCommerce [product_page] existing shortcode in the loop
            $output .= '<div class="product-'.$post_id.'">' . do_shortcode ( "[product_page id=$post_id]" ) . '</div>';
        }

        $output .= '</div>'; // DIV container html tag (closing)

        // Always return the output (never echo)
        return $output;
    }

    add_shortcode( 'product_pages_cat', 'single_product_pages_by_category' );
}

下面是一个自定义短代码,它在循环中使用现有的woocommerce短代码,以输出定义的产品类别(一个接一个)的所有单个产品页面

此自定义短代码有2个参数:

  • cat
    用于产品类别(slug)
  • 限制要显示的帖子数量(默认为全部)
守则:

if( !function_exists('single_product_pages_by_category') ) {

    // Add Shortcode
    function single_product_pages_by_category( $atts ) {

        // Attributes
        $atts = shortcode_atts(
            array(
                'cat' => '',
                'limit'   =>'-1'
            ),
            $atts, 'product_pages_cat'
        );

        $posts = get_posts( array(
            'post_type'      => 'product',
            'post_status'    => 'publish',
            'posts_per_page' => $atts['limit'],
            'tax_query'      => array( array(
                'taxonomy'   => 'product_cat',
                'field'      => 'slug',
                'terms'      => $atts['cat'],
            ) )
        ) );

        $output = '<div class="products-in-'.$atts['cat'].'">'; // DIV container html tag (opening)

        // Iterating though each product in the defined category
        foreach( $posts as $post ){
            $post_id = $post->ID; // The Product ID
            // Using WooCommerce [product_page] existing shortcode in the loop
            $output .= '<div class="product-'.$post_id.'">' . do_shortcode ( "[product_page id=$post_id]" ) . '</div>';
        }

        $output .= '</div>'; // DIV container html tag (closing)

        // Always return the output (never echo)
        return $output;
    }

    add_shortcode( 'product_pages_cat', 'single_product_pages_by_category' );
}

是的,这正是我要找的。非常感谢!事实上,它并没有像我想象的那样100%有效。它在每个产品下方显示每个产品,但缺少描述,并且在显示的每个产品下方始终显示“类似产品”。是否可以将“类似产品”仅放在帖子末尾的最后一个产品下方一次?为展示的每一件产品添加描述?这就是它现在的样子:嘿。非常感谢!事实上,它并没有像我想象的那样100%有效。它在每个产品下方显示每个产品,但缺少描述,并且在显示的每个产品下方始终显示“类似产品”。是否可以将“类似产品”仅放在帖子末尾的最后一个产品下方一次?为展示的每件产品添加描述?这就是目前的情况: