I'm developing a medical directory website in Wordpress, and in the Grid/List pages for the site, each of the listing cards contains a list of specialties (using the category taxonomy within the dDoctors/Directorist theme & plugin). Currently, these specialties are displaying in each of the grid cards in alphabetical order in a Flexbox wrap. Because of the limited space, sometimes there might be some wasted white space in the container. Case in point the following grid card which by default appears as so:
<ul>
<li>Emergency</li>
<li>Hospice Home Care</li>
<li>ICU (NICU)</li>
<li>Med/Surg</li>
<li>OB</li>
<li>Oncology</li>
</ul>
Which looks like this on the page:
I used Developer Tools to rearrange the items so that they're listed in order of least to most character count per category, showing like this in code:
<ul>
<li>OB</li>
<li>Med/Surg</li>
<li>Oncology</li>
<li>ICU (NICU)</li>
<li>Emergency</li><li>Hospice Home Care</li>
</ul>
and the code generated on the page as so (with one less line, SCORE!):
So clearly, it appears if I want to optimize the display of these items, they should be arranged in character count order, but to my knowledge, neither Wordpress nor my plugin/theme support this arrangement by default.
The PHP loop that generates this list is located inside the plugins/directorist/templates/archive/fields/category.php file, and looks like this:
<ul>
<?php foreach ( $listings->loop['cats'] as $cat ) {
$output = '<li>' . $cat->name . '</li>';
echo $output;
};
?>
</ul>
How can I get this list to be sorted by character count? I would guess I would need to declare a variable for the character count, and then generate a new foreach that sorts it by that. Do I have that correctly, and how would I go about doing that?