1

I have the below code:

Here I declare the code for getting the category name and slug

    $args = array(
            'orderby' => 'name',
            'order' => 'ASC',
            'hide_empty' => 0,
        'taxonomy'           => 'category'
        );
        $categories = get_categories($args);
        $categories_name = array(); // here i get the category name
    $categories_ids = array(); // here i get the category slug
    foreach($categories as $category){
          $categories_name[] = $category->name;
      $categories_ids[] = $category->slug;
        }

Here is the setting that is echoed in my back-end with the category name for the user to select

    $this->settings['categoriesmain3'] = array(
          'section' => 'general',
          'title'   => __( 'Select Right Block Category' ),
          'desc'    => __( 'Here you can select the main category for main right block.' ),
          'type'    => 'select',
          'std'     => '',
          'choices' =>  array('$categories_name' => '$categories_ids') //I am trying to do 
        );
    $settings = get_option('mytheme_options');
    $my_choices_cat3  = $settings['categoriesmain3'];

I am trying to add in choices => the code array('$categories_name' => '$categories_ids')' but it doesn't work like that. In my example for manually adding the select field I have 'choices' => array('Choice 1' => 'choice1', 'Choice 2' => 'Choice 2'), so what is the correct syntax for the choices element where i want to add a dynamic list?

1 Answer 1

2

This can be solved by creating one array, using the category names as keys and the category ids as values.

$categories = get_categories($args);
$categories_options = array();

foreach($categories as $category){
  $categories_options[$category->name] = $category->slug;
}

You can then use the following line to add the list.

'choices' => $categories_options
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.