WordPress定制器:带有类别输出的下拉列表

时间:2017-03-23 作者:Rico Weigand

我编写了以下几行代码。它显示了一个下拉菜单,其中包含Wordpress定制器中所有可用的Wordpress类别,并可以输出类别slug。但是我想输出类别id,而不是slug。有人知道如何实现这一点吗?

$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
)));

3 个回复
最合适的回答,由SO网友:LWS-Mo 整理而成

在foreach循环中,只需快速print_r($category) 查看所有可用选项。

你会发现你可以使用$category->term_id 获取ID 术语/类别的,而不是$category->slug.

例如,通过使用上面的代码:

$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 ) 

SO网友:Mohammad Ayoub Khan

我有一个解决办法

第一次创建节

// 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
]);

SO网友:Mohammad Ayoub Khan

我有另一个解决方案

<?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(),
]);
?>