admin管理员组文章数量:1290263
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
)));
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
)));
Share
Improve this question
asked Mar 23, 2017 at 18:47
Rico WeigandRico Weigand
33 bronze badges
1
- A possible solution: ralphjsmit/… – Jesse Nickles Commented May 22, 2023 at 21:24
3 Answers
Reset to default 0In 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 )
I have a solution to this
- First create section
// FRONT Page Settings settings.
$wp_customize->add_section(
'betacoders_theme_front_page_options',
array(
'title' => __('
本文标签:
categoriesWordpress Customizer Dropdown with Category output
版权声明:本文标题:categories - Wordpress Customizer: Dropdown with Category output 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1741492553a2381690.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论