admin管理员组文章数量:1333412
I want to create a selector control in the Wordpress customizer with all the existing categories and for that I need an array with all the categories and their names.
I need to get an array like this:
[
'category_one' => esc_html__( 'Category name 1', 'kirki' ),
'category_two' => esc_html__( 'Category name 2', 'kirki' ),
'category_three' => esc_html__( 'Category name 3', 'kirki' ),
'category_four' => esc_html__( 'Category name 4', 'kirki' ),
],
(I'm using the Kirki framework, but that's AFAIK not very relevant to this question, because it is only about getting the array).
I figured out I could do it with something like this:
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
but that gives me a way too complex array with too much information.
If it is possible, it would be great if the same could also be done with tags and then combined into one array. The combining can be done with a PHP-operator, it's really about getting an array with the category and the name.
Thanks a lot in advance!
I want to create a selector control in the Wordpress customizer with all the existing categories and for that I need an array with all the categories and their names.
I need to get an array like this:
[
'category_one' => esc_html__( 'Category name 1', 'kirki' ),
'category_two' => esc_html__( 'Category name 2', 'kirki' ),
'category_three' => esc_html__( 'Category name 3', 'kirki' ),
'category_four' => esc_html__( 'Category name 4', 'kirki' ),
],
(I'm using the Kirki framework, but that's AFAIK not very relevant to this question, because it is only about getting the array).
I figured out I could do it with something like this:
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
but that gives me a way too complex array with too much information.
If it is possible, it would be great if the same could also be done with tags and then combined into one array. The combining can be done with a PHP-operator, it's really about getting an array with the category and the name.
Thanks a lot in advance!
Share Improve this question asked Jun 20, 2020 at 14:59 ralphjsmitralphjsmit 4026 silver badges23 bronze badges1 Answer
Reset to default 1This part of code from the question will fetch only parent category because 'parent' => 0
, drop this from array if you need all the categories
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
// You can iterate over the list of objects returned by `get_categories`
// to achieve list of categories in required format.
$category_list = array();
foreach( $categories as $category ) {
$category_list[$category->slug] = esc_html__( $category->name, 'kirki' );
}
本文标签: tagsHow to get an array with all categories and corresponding names
版权声明:本文标题:tags - How to get an array with all categories and corresponding names? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331266a2454741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论