wordpress获取所有分类ID函数get_all_category_ids()
WordPress does not have a builtin function named get_all_category_ids()
. However, you can easily retrieve all category IDs by using a custom function that queries the WordPress database. Here's an example of how you can create such a function:
function get_all_category_ids() {
$args = array(
'taxonomy' => 'category',
'hide_empty' => false,
);
$categories = get_categories($args);
$category_ids = array();
foreach ($categories as $category) {
$category_ids[] = $category>term_id;
}
return $category_ids;
}
Here's how this function works:
-
It defines an array of arguments for the
get_categories()
function. Thetaxonomy
argument is set to'category'
, andhide_empty
is set tofalse
to include categories that have no posts. -
It calls
get_categories()
to retrieve all categories based on the provided arguments. -
It loops through the returned categories, extracts the
term_id
(which is the category ID), and adds it to an array. -
Finally, it returns the array of category IDs.
You can use this get_all_category_ids()
function in your WordPress theme or plugin to get an array of all category IDs on your site.
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。