I’m using the code below to display both top-level pages and categories in my menu. (If this is the incorrect way to do so, please let me know). I would like to arrange the items alphabetically. The problem being that if I add the argument to each set of parentheses, I end up with two separate lists arranged alphabetically – I would like to combine the items in each list and arrange alphabetically from there. I’m not sure where I go from here and would appreciate any help!
<?php wp_list_pages('title_li&depth=1'); wp_list_categories('title_li&depth=1'); ?>
Problem is that functions you are using output html as string, so it’s hard to order them. Is possible, maybe using an html parser, but there are some simpler alternatives.
First is surely using the core menu features, but I know that is not possible using it from code if a menu is not created in the backend, and if you are looking a function that run without any configuration, that can’t be the solution.
Another approach is to use functions that retrieves pages and category, merge them, order alphabetically and finally generate the html output.
It’s easiers than it can seems, you need only a function:
To use it just put
to replace your actual
Further reading:
get_pages
get_categories
ksort