This is probably more a question about PHP rather than WordPress, and as such may be closed as off-topic for this site.
That said, you can usort()
to sort an array using a custom comparison function:
usort(
$listings->loop['cats'],
function( $a, $b ){
return strlen( $a->name ) - strlen( $b->name );
}
);
After the call above, the $listings->loop['cats']
array will be ordered from smallest category name to largest.
Sorting from shortest to longest will pack all of the smallest items together nicely – but it also concentrates the largest items, which may ultimately take up an entire line a piece if not paired with a short item. A more space-efficient approach is likely one which would mix long and short items, but it might not feasible if you don’t have a rough measure of the width constraint beforehand.
Related Posts:
- Listing all posts from current category on page
- Taxonomy name repeating when showing 2 posts from each category
- How to get posts using category slug in ClassiPress?
- Category post count is not correct
- Display Taxonomies in loop with template args
- Is There a Difference Between Taxonomies and Categories?
- How to check if I’m on the last page of posts?
- Check if a post is in any child category of a parent category
- How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms
- How can I add the featured image functionality to a custom taxonomy?
- Share one taxonomy across multiple blogs in 3.0 [duplicate]
- wp_update_nav_menu_item() to insert categories
- Admin Panel – Disable Moving Selected Terms To Top of Metabox
- How to make custom taxonomy check boxes like ‘Categories’
- How to create non-unique sub-category slugs?
- Loop with Dynamic Categories
- How can I create a loop to build slides based on multiple categories using Coda Slider
- How to get next previous category in same taxonomy?
- Display only deepest category on a single post?
- Categories list loop – add separator every year
- How would you create a “weekly” archive?
- How to exclude categories from recent posts, recent comments & category widgets?
- How to add HTML5 ‘required’ attribute to wp_dropdown_categories() without JavaScripts?
- Wrap a chosen category name with div
- Dropdown category filter
- How to query posts from specific authors and categories using WP_query?
- Listing Parent, Child and GrandChild Categories and then the PostTitles on Page Template !
- Add custom taxonomy under the category title
- List Terms in Category
- How to get category URL with the slug?
- Display Slug instead of Name
- ACF: Display category name using taxonomy field
- Class active for Archive link – looped out
- New Categories Not Shown In the Post Edit Page
- Shortcodes not working in category or tag description
- Filtering multiple categories via the URL
- Display the last post by each category?
- Display specific categories by ID
- Counter in loop in foreach
- Display Posts Loop for each parent and for each Child
- How to exclude the last post from a category(featured for example) from the main loop?
- Flat category URLs but retaining hierarchy?
- Order by category titles
- ajax for filtering posts by category in wordpress loops
- How to use an array of categories as a dropdown?
- Custom taxonomy template for categories
- Wacky taxonomy in wordpress
- Question on using custom structures for categories
- Category archive is displaying all posts rather than the specific category
- new WP_Query all post in a category inside the loop
- How to add orderby parameter for sorting on category.php
- How to modify the output of wp_terms_checklist when used within the built-in category metabox on edit posts?
- issue with if/elseif in_array inside foreach loop display only one post
- How to get an array with all categories and corresponding names?
- List just subcategory and products of active category page in Woocommerce
- How to display the parent category if only the child category has been chosen
- Category tags with comma’s
- Remove any product that is featured from regular display loop [WooCommerce]
- Need to echo category id in multi-dimensional array
- More efficient way to list posts by category [duplicate]
- Category nicename inside loop in wordpress
- filter a loop base on specific category
- Setting selected term_id with wp_dropdown_categories?
- creating categories programmatically
- Editing Loop So It Targets Specific Tags?
- Problem: retrieving parent category’s first post
- Posts made to category don’t show up (do show up on frontpage)
- Display all categories but only if they have posts in them that have a specific tag assigned
- Include last post date in get_categories loop
- How can i change sort category view starting from child then parent, not alphabetically
- How to get specific (grand parent) category of current post?
- how tho change number of posts in loop from specific categories
- error in specific category loop
- loop in single.php of the same category
- Bulk assign posts to a category using SQL (MySQL)
- Hide a category/posts from the homepage in wp
- outputting taxonomy hierarchy
- Filter custom taxonomy posts via AJAX
- Why use hierarchical taxonomies instead of many custom taxonomies?
- How get all media from a posts category by db?
- Cant get unique_array() work on get_the_category() foreach loop
- Trying to display all posts in a category
- How to exclude a specific categogy from a custom page template
- Match two taxonomies to display a specific content
- How to get multiple loop in category.php, my scripts becomes madness
- Custom Category Page Not Working [duplicate]
- My Query is getting the wrong data
- Return selected categories only in custom page meta box?
- Custom loop by url
- How to change category id on get_posts array
- Chained drop-down menu for custom taxonomy using chained.js
- Display only one level subcategory in wordpress
- Do not show all post from all categories, just show posts from category in current loop
- IF have_posts contains posts from a certain category
- Echo Category In Loop
- How to define category ID in an array?
- hide_empty property not working when using get_terms
- Get categories by title descendant
- Add custom fields to Woocommerce Category Page
- How to make /category/ URL load properly instead of 404 error?