Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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 商业上相同子类别的4个相关产品_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 商业上相同子类别的4个相关产品

Php 商业上相同子类别的4个相关产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想在页面底部展示4种产品,展示相关产品。只是同一子类别上的产品。为了实现这一点,我在related.php上做了如下更改 $cats_array = array(4); // Get categories $terms = wp_get_post_terms( $product->id, 'product_cat' ); //Select only the category which doesn't have any children foreach ( $terms as $te

我想在页面底部展示4种产品,展示相关产品。只是同一子类别上的产品。为了实现这一点,我在related.php上做了如下更改

$cats_array = array(4);

// Get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );

//Select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}

$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
 array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
) );
它只显示与子类别相关的产品。问题是它有时显示2,有时显示3。。。即使重新加载相同的产品,它也必须始终为4

我怎样才能做到这一点

谢谢