Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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 foreach循环强制某些子项位于顶部_Php_Wordpress_Woocommerce - Fatal编程技术网

PHP foreach循环强制某些子项位于顶部

PHP foreach循环强制某些子项位于顶部,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,下面的foreach循环运行在一系列WooCommerce产品上,这些产品通过下面的查询进行查询。这将输出菜单中的产品_order,这与预期一样,但是我需要做的是将它们保持在此顺序,但作为“特色产品”的产品除外。order参数几乎应该忽略这些参数,并始终首先出现在foreach循环中 这可能吗 // Get Products $args = array( 'status' => 'publish', 'category' => $currentCat->slug,

下面的
foreach
循环运行在一系列WooCommerce产品上,这些产品通过下面的查询进行查询。这将输出
菜单中的产品_order
,这与预期一样,但是我需要做的是将它们保持在此顺序,但作为“特色产品”的产品除外。order参数几乎应该忽略这些参数,并始终首先出现在
foreach
循环中

这可能吗

// Get Products
$args = array(
  'status' => 'publish',
  'category' => $currentCat->slug,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'limit' => -1,
);
$productsData = wc_get_products( $args );

<? // For each product
foreach($productsData as $product): ?>
        
  <? if( $product['isFeatured'] ): ?>
    // Output featured product html here...
  <? else: ?>
    // Output other products HTML here...
  <? endif ?>
<? endforeach ?>
//获取产品
$args=数组(
“状态”=>“发布”,
“类别”=>$currentCat->slug,
'orderby'=>'菜单\u顺序',
“订单”=>“ASC”,
“限制”=>-1,
);
$productsData=wc\U get\U产品($args);
//在此处输出特色产品html。。。
//在此处输出其他产品HTML。。。

我还没有试过,但你可以看看这是否有效。我知道这个技巧适用于元查询,所以它可能也适用于分类查询:技巧是在orderby中按名称引用tax查询

$featured_tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN',
);
$non_featured_tax_query = $featured_tax_query;
$non_featured_tax_query['operator'] => 'NOT IN';

$args = array(
  'status' => 'publish',
  'category' => $currentCat->slug,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'limit' => -1,
  'tax_query' => array('featured' => $featured_tax_query,
                       'non_featured' => $non_featured_tax_query),
  'orderby' => array(
    'featured' => 'ASC',
    'non_featured' => 'ASC',
  )
);
$featuredProductsData = wc_get_products( $args );
如果上述方法不起作用,您可以将其分解为两个不同的查询,如下所示:

$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN',
);

// Get featured products
$args = array(
  'status' => 'publish',
  'category' => $currentCat->slug,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'limit' => -1,
  'tax_query' => $tax_query,
);
$featuredProductsData = wc_get_products( $args );

foreach($featuredProductsData as $product): ?>
    // Output featured product html here...
<?php endforeach; ?>

// Get non-featured products
$tax_query['operator'] => 'NOT IN';
$args['tax_query'] => $tax_query;
$productsData = wc_get_products( $args );

foreach($productsData as $product): ?>
    // Output other products HTML here...
<?php endforeach; ?>
$tax\u query[]=数组(
“分类法”=>“产品可视性”,
'字段'=>'名称',
“术语”=>“特色”,
'运算符'=>'在'',
);
//获得特色产品
$args=数组(
“状态”=>“发布”,
“类别”=>$currentCat->slug,
'orderby'=>'菜单\u顺序',
“订单”=>“ASC”,
“限制”=>-1,
“tax\u query”=>$tax\u query,
);
$featuredProductsData=wc\u get\u产品($args);
foreach($featuredProductsData作为$product):?>
//在此处输出特色产品html。。。
//获取非特色产品
$tax_query['operator']=>'不在';
$args['tax\u query']=>$tax\u query;
$productsData=wc\U get\U产品($args);
foreach($PRODUCTS数据作为$product):?>
//在此处输出其他产品HTML。。。

通过执行两个单独的查询,然后将它们合并到一个数组中(按所需顺序),根据我的需要解决了这个问题。这确保了一个查询总是出现在另一个查询之前,而每个查询仍然可以遵循排序参数,例如
菜单\顺序

// Featured products query
$featuredArgs = array(
  'status' => 'publish',
  'category' => $currentCat->slug,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'limit' => 1,
  'tax_query' => array(
    array(
      'taxonomy' => 'product_visibility',
      'field'    => 'name',
      'terms'    => 'featured',
      'operator' => 'IN',
    ),
  ),
);

// Non-featured products query
$nonFeaturedArgs = array(
  'status' => 'publish',
  'category' => $currentCat->slug,
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'limit' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'product_visibility',
      'field'    => 'name',
      'terms'    => 'featured',
      'operator' => 'NOT IN',
    ),
  ),
);

// Get products from each query (performed seperately so that we can control the order of products)
$featuredProductsData = wc_get_products( $featuredArgs );
$nonFeaturedProductsData = wc_get_products( $nonFeaturedArgs );

// Merge both queries in the desired order
$productsData = array_merge($featuredProductsData, $nonFeaturedProductsData);

我得去睡觉了,但看看“粘性”产品,你应该会找到一些答案。嘿@wp-overwatch.com-谢谢这两个选项。我认为最好的选择看起来很有希望,但目前还不太管用。“特色”和“非特色”名称来自哪里?目前,这两个单独的税务查询单独工作,但当使用“特色”和“非特色”名称组合时,不工作?