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 )
Related Posts:
- Dropdown with category selection
- View category in the preview of Theme Customizer
- Saving two categories from two dropdowns in front end posting form
- Get id of category from drop down menu
- How to add HTML5 ‘required’ attribute to wp_dropdown_categories() without JavaScripts?
- How to retain the values from dropdown category lists after wrong form submission?
- Help with multiple dropdown tags search
- How to use wp_category_checklist()?
- wp_dropdown_categories and custom taxonomy + custom post type
- How to provide value for ‘selected’ on wp_dropdown_categories() for ‘multiple’ options
- Additional form options based on category selected not working
- wp_dropdown_categories() scripts working erratically on sidebar
- How to use an array of categories as a dropdown?
- How do I display only two categories Instead of displaying all categories
- Dropdown Categories used in form shows no results when all is selected
- Trying to get variables in hacked category dropdown
- wp_list_categories walker without links on categories that have subcategories (to make a nested dropdown menu)
- How to get an array with all categories and corresponding names?
- Filter posts by categories ajax is showing nothing
- Filter By Category Dropdown List With Custom Taxonomies
- A dropdown with checklist
- Wp Dropdown Category Not Show Current As Title
- wp_dropdown_categories initial value
- Exclude category from drop down list form
- Trying to get categories and sub-category listing
- Get_term_meta() always returns false
- Woocommerce Category Sort Dropdown
- How to add custom option to wp_dropdown_categories?
- custom browse by
- Get category fixed in dropdown browse categories
- Categories dropdown with all values selected using wp_dropdown_categories
- add custom option to get_categories dropdown
- Taxonomy for custom post type not found
- Form Value of drop down category + Translation of ‘show option none’
- How to add featured image for category (without a plugin)?
- Category archive by year with permalink support /category/YYYY
- How do I get the category URL from get_the_category?
- How do I use the same post slug for different Categories?
- Default WP Gallery – show only galleries of a certain category
- How to tax query X number of posts related by tag first, then by category if not enough in tag-related
- Exclude sub category posts from category display
- How to hide a specific category posts in my monthly archive?
- Automated adding of one tag to all the posts in a category
- Deleting All tags except categories Wp database
- Action hooks returning old category instead of new category
- Category checkboxes in upload modal
- How to mass-insert categories?
- What is the advantage of using header-catname.php over is_category(‘catname’);?
- Custom WP_Query for current category (in category.php)?
- Why is get_the_category() saying that I have two categories?
- Custom sidebar on category pages
- How do I get the category slug from wp_dropdown_categories
- Change the category of all posts in it
- How to display only posts assigned to a particular, isolated, subcategory
- How to hide some categories in category list under post in wordpress?
- exclude categories from search results
- How to categorize posts under Category 1 and Category 2 which are separate lists of categories, and display them as separate lists in widgets
- Creating Category Specific menus
- Category archives with monthly pagination
- Older entries link within category page
- Two separate portfolio pages [closed]
- How to do some action weekly?
- How to hide child categories from displaying
- How to allow Category Title to use tags?
- List Child Categorie and hide the parent
- Child categories doesn’t echo if it doesn’t contain at least a post
- Show different title on category page
- Notice: Undefined variable: category_id
- Is it possible to create an alias/custom taxonomy for a category name?
- Style a category from category list
- Adding a page summary to a specific page
- Add a category on all archive category pages
- Multiple category columns and post counting list in wordpress Homepage
- Check for parent category
- add current-cat class to single post page
- Problem with wp_create_category
- Problem with pagination block in Gutenberg
- Product category display in tabs
- Sending all categories associated with a post to Google Ad Manager for use with .setTargeting
- Pull in an “Include” file based on a WordPress Category ID
- Sort categories by custom field in WordPress admin
- Why is URL Slug Saving Differently From How It’s Been Defined?
- How to set acf color field as background color to product category
- How to add all subcategories as submenu in WordPress menu
- Related to genre and category
- Get permalink to latest post in category
- How to noindex, follow a specific category wordpress?
- get taxonomy list in a page in the wordpress
- Is there a way to allow users to customize their feed view?
- How to load terms of a custom taxonomy of a product in woocommerce cart page
- How to exclude category and post_tag taxonomy while displaying custom taxonomy?
- Group Posts by First Letter of Title categories
- How to make 10 post of one category
- Listing category
- Anyway to hide a Category in the Categories section when adding/editing a post in WP Admin?
- Showing one post from each category, paged?
- Custom permalinks structure
- Weird html output of single_cat_title – is not inside of the html element?
- sub-category showing up in wrong order in breadcrumbs
- Hierarchical outline of posts