最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tags - How to get an array with all categories and corresponding names?

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

This 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' );
}
发布评论

评论列表(0)

  1. 暂无评论