0

I wrote the following lines of code. It shows a dropdown menu with all available Wordpress categorys in the Wordpress customizer and can output the category slug. But instead of the slug I want to output the category id. Does anyone have an idea how to realize that?

$categories = get_categories();

$cats = array();
$i = 0;
foreach($categories as $category){
    if($i==0){
        $default = $category->slug;
        $i++;
    }
    $cats[$category->slug] = $category->name;
}

$wp_customize->add_setting('wptimes_homepage_featured_category', array(
    'default'        => $default
));

$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'wptimes_homepage_featured_category', array(
    'label' => 'Hervorgehobene Kategorie',
    'description' => 'Wähle hier die Kategorie aus, die du auf der Startseite hervorheben möchtest.',
    'section' => 'wptimes_homepage',
    'settings' => 'wptimes_homepage_featured_category',
    'type'    => 'select',
    'choices' => $cats
)));
1

3 Answers 3

0

In your foreach loop just do a quick print_r($category) to see all available options.

Than you will see that you can use $category->term_id to get the ID of the term/category, instead of $category->slug.

So for example, by using your code from above:

$categories = get_categories();

$cats = array();
$i = 0;

foreach( $categories as $category ) {

    // uncomment to see all $category data
    #print_r($category);

    if( $i == 0 ){

        $default = $category->term_id;
        $i++;

    }
    $cats[$category->term_id] = $category->name;
} 

print_r($cats);
// Prints for example: Array ( [12] => Child-Cat [2] => Parent-Cat [1] => Uncategorized ) 
0
1

I have a solution to this

  1. First create section
// FRONT Page Settings settings.
$wp_customize->add_section(
  'betacoders_theme_front_page_options',
  array(
    'title'       => __('📝 - Front Page Settings ', 'betacoders'),
    'capability'  => 'edit_theme_options',
    'description' => __('FRONT Page Layout Options', 'betacoders'),
    'priority'    => 2,
  )
);
//  ==================================
//  = Featured Categories SELECTOR   =
//  ==================================
$wp_customize->add_setting('featured_post', array(
  'capability' => 'edit_theme_options',
  'type'       => 'option',
));

// get categories list
$categories = get_categories(array(
  'orderby' => 'name',
  'order'   => 'ASC',
  'hide_empty'  => 0,
  // 'parent'  => 0,
  // 'hierarchical' => true
));
$cat_ids = array_map(function ($el) {
  return $el->cat_ID;
}, $categories);

$cat_names = array_map(function ($el) {
  return $el->cat_name;
}, $categories);

$wp_customize->add_control('mys_featured_post_ctrl', [
  'label' => 'Featured Post from Category',
  'description' => 'Featured Post from Category',
  'section'    => 'betacoders_theme_front_page_options',
  'settings' => 'featured_post',
  // 'type' => 'select',
  'type'        => 'checkbox',
  'choices' => array() + array_combine($cat_ids, $cat_names) // combines all categories data
]);
0

I have another solution

<?php
function get_categories_select()
{
  $teh_cats = get_categories();
  $results = [];

  $count = count($teh_cats);
  for ($i = 0; $i < $count; $i++) {
    if (isset($teh_cats[$i]))
      $results[$teh_cats[$i]->slug] = $teh_cats[$i]->name;
    else
      $count++;
  }
  return $results;
}
$wp_customize->add_setting('featured_post_1', array(
  'capability' => 'edit_theme_options',
  'type'       => 'option',
));
$wp_customize->add_control('betacoders_featured_post_2', [
  'label' => 'Featured Post from Category 2 ',
  'description' => 'Featured Post from Category 2 ',
  'section'    => 'betacoders_theme_front_page_options',
  'settings' => 'featured_post_1',
  'type' => 'select',
  // 'type'        => 'checkbox',
  'choices' => get_categories_select(),
]);
?>

Your Answer

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

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