从 get_categories 检索一个 id 数组
retrieve an array of id's from get_categories
我正在尝试从以下位置检索 ID 数组:
$categories = get_categories(array('hide_empty' =>false,'taxonomy'=>'product_cat', 'child_of' => 15));
当我用 var_dump 检查 $categories 时,我得到了一切。
但只需要term_ID。
我尝试了以下 foreach 循环:
foreach ($categories as $category) {
$option = $category->cat_ID;
var_dump($option);
}
var_dump 现在显示:
int(16) int(37) int(41) int(42) int(17) int(38) int(29) int(40) int(30) int(31) int(32) int(33) int(34) int(36)
我假设这就是所需的数组。以后如何将此数组传递给条件?
西奥
在经历了很多困惑之后,我想理清这个问题,post 对我有用的是:
<?php
$categoryX = [];
$parentID = XX; //id of parent
$category = get_categories( array(
'hide_empty' =>false,
'taxonomy'=>'product_cat',
'child_of' => $parentID
) );
foreach( $categoriySort as $category ) {
array_push($categorySortiment, $category->cat_ID);
}
array_push($categorySortiment, $parentID);
然后我可以将包含所有 ID 的数组的变量 $categoryX 分配给稍后的条件。
感谢您的帮助。
下面声明了一个新数组,然后遍历类别,使用 PHP 的 array_push() 函数将 $category->cat_ID 值添加到 $categoryIDArray。这会生成一个仅包含类别 ID 的数组。
$categoryIDArray = [];
foreach ($categories as $category) {
array_push($categoryIDArray, $category->cat_ID);
}
然后您可以简单地将 $categoryIDArray 传递给您需要的任何函数,如下所示:
$categories = getCategories( $categoryIDArray );
$all_Categories = 数组();
foreach ($categories 作为 $category) { $all_Categories = $category->cat_ID;}
我正在尝试从以下位置检索 ID 数组:
$categories = get_categories(array('hide_empty' =>false,'taxonomy'=>'product_cat', 'child_of' => 15));
当我用 var_dump 检查 $categories 时,我得到了一切。 但只需要term_ID。
我尝试了以下 foreach 循环:
foreach ($categories as $category) {
$option = $category->cat_ID;
var_dump($option);
}
var_dump 现在显示:
int(16) int(37) int(41) int(42) int(17) int(38) int(29) int(40) int(30) int(31) int(32) int(33) int(34) int(36)
我假设这就是所需的数组。以后如何将此数组传递给条件? 西奥
在经历了很多困惑之后,我想理清这个问题,post 对我有用的是:
<?php
$categoryX = [];
$parentID = XX; //id of parent
$category = get_categories( array(
'hide_empty' =>false,
'taxonomy'=>'product_cat',
'child_of' => $parentID
) );
foreach( $categoriySort as $category ) {
array_push($categorySortiment, $category->cat_ID);
}
array_push($categorySortiment, $parentID);
然后我可以将包含所有 ID 的数组的变量 $categoryX 分配给稍后的条件。
感谢您的帮助。
下面声明了一个新数组,然后遍历类别,使用 PHP 的 array_push() 函数将 $category->cat_ID 值添加到 $categoryIDArray。这会生成一个仅包含类别 ID 的数组。
$categoryIDArray = [];
foreach ($categories as $category) {
array_push($categoryIDArray, $category->cat_ID);
}
然后您可以简单地将 $categoryIDArray 传递给您需要的任何函数,如下所示:
$categories = getCategories( $categoryIDArray );
$all_Categories = 数组(); foreach ($categories 作为 $category) { $all_Categories = $category->cat_ID;}