Reputation: 1449
I am using User-Submitted-Posts Plugin in order to let users submit their images to my wordpress site. Each image is uploaded within a category. The categories are set in the wordpress admin area and are pulled into the submission form using the following code.
My only problem is that this web form pulls the categories through ordered by their category ID not in alphabetical order. Therefore when i ADD a category even if it begins with the letter A, it will appear at the bottom.
Can anyone let me know how to edit the following code to make all categories list in ASC alphabetical order?
<li class="usp_category">
<label for="user-submitted-category" class="usp_label"><?php _e('Categories'); ?></label>
<div>
<select class="usp_select" name="user-submitted-category" id="user-submitted-category">
<?php foreach($settings['categories'] as $categoryId) { $category = get_category($categoryId); if(!$category) { continue; } ?>
<option class="usp_option" value="<?php echo $categoryId;?> "><?php $category = get_category($categoryId); echo htmlentities($category->name); ?></option>
<?php } ?>
</select>
</div>
</li>
Upvotes: 0
Views: 2803
Reputation: 38
I can't actually try this, and i have no ideea how your plugin works... but the easiest solution that comes to my mind is to do 2 foreach loops. This would be inside your <select></select> :
<?php
$cats = array();
// get what you need in an array
foreach($settings['categories'] as $catid) {
$cat = get_category($catid);
if(is_object($cat)) $cats[$catid] = $cat->name; // why htmlentities() ?
}
// sort by name
asort($cats);
foreach($cats as $catid => $catname)
echo "<option class=\"usp_option\" value=\"{$catid}\">{$catname}</option>";
?>
hope it works/helps
Upvotes: 1