Return array of categories to php function
-
16-10-2019 - |
Question
I am looking for a function that will retrieve the names and ids of all categories that will output a checkbox, or give me the data so I can perform a loop.
<input type="checkbox" name="category" value="category_id" /> Category 1
<input type="checkbox" name="category" value="category_id" /> Category 2
<input type="checkbox" name="category" value="category_id" /> Category 3
<input type="checkbox" name="category" value="category_id" /> Category 4
<input type="checkbox" name="category" value="category_id" /> Category 5
Solution
this should do it:
$categories = get_categories();
foreach( $categories as $category ) {
echo '<input type="checkbox" name=' . $category->slug . '" value="' . $category->term_id . '" /> ' . $category->name . '<br />' . "\n";
}
and you can change/order the list by feeding arguments to get_categories()
:
http://codex.wordpress.org/Function_Reference/get_categories
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange