0

I need to output information about each of my post categories in this array, but I'm having trouble figuring out how.

I need to output each category as an array (See examples) under the values option with the following information;

'label' => 'CATEGORY NAME' 'values' => 'CATEGORY ID'

array(
    'param_name'  => 'ids',
    'settings'    => array(
        'multiple' => true,
        'values'   => array( 
            // EXAMPLE
            array( 'label' => 'Abrams', 'value' => 1, 'group' => 'category' ),
            // EXAMPLE
            array( 'label' => 'Brama', 'value' => 2, 'group' => 'category' )
        ),
    ),
),

I've tried using a foreach get_categories() loop inside the array but apparently PHP doesn't like that.

Any help would be greatly appreciated.

4
  • Do you need this information inside post object right?
    – Sonali
    Feb 24, 2017 at 11:23
  • @sonalip3 no. This is inside a custom function Feb 24, 2017 at 11:26
  • Why doesn't get_categories() work?
    – Svartbaard
    Feb 24, 2017 at 12:49
  • @Svartbaard because I need to output the data in a specific format array( 'label' => '[CATEGORY NAME]', 'value' => [CATEGORY ID], 'group' => 'category' ) Feb 24, 2017 at 13:17

1 Answer 1

1

Try something like the following. Notice that the hide_empty argument is set to false.

$args = array(
    'orderby' => 'id',
    'hide_empty'=> false,
);

$cats = get_categories($args);

foreach ($cats as $cat) {
    // Your code to populate new array here
}

And then assign your newly created array to values.

7
  • So in place of 'values' => array(...) I'd put 'values' => $cats? Feb 24, 2017 at 13:18
  • That was my thought process yes. Populate the array the way you need it first and assign it to values.
    – Svartbaard
    Feb 24, 2017 at 13:19
  • I tried that, but it didn't output any information. Feb 24, 2017 at 13:21
  • Try the following and see what it does: echo '<pre>'; print_r($cats); echo '</pre>'; foreach ($cats as $cat) { echo $cat->name; } I think the reason you didnt get results is due to the hide_empty argument.
    – Svartbaard
    Feb 24, 2017 at 13:23
  • It outputs the default list of data that get_categories outputs. I need to use certain values out of this data, in another array Feb 24, 2017 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.