I am working on a website that is using posts as a way to manage glossary articles similar to the example website below. I would like to find a way to display articles alphabetically and grouped by a selected letter (such as the letter ‘F’ in the example). I would like the process to be automatic.
Example of desired output: http://www.retirementdictionary.com/glossary/f
Can anyone suggest a way to do this?
once up a time i did a client project where i had to have archives by first letter. thinking back i’m wondering if shouldn’t have just created a hidden taxonomy and then saved the first letter as a term in that taxonomy.
anyway, here’s what i actually did:
if you have posts already before adding this you’ll need to run the following once to grab the first letters for the existing posts:
now we need some stuff to decipher when were are on a custom archive page and what to do differently
little helper function to make links
now create our custom archive menu
clicking on the links should now bring you to a page that only shows posts that state with that letter.
looking back i think the taxonomy idea would be a lot less code and have cleaner rewrite support built-in from the beginning (ie… no query vars, even though those could be re-written… i don’t know how). the taxonomy approach would also add tax data to the DB, whereas this only adds the one option. so trade-off?
*EDIT**
ok, i took a stab at the taxonomy route and it is slightly more elegant like i expected
first register the taxonomy. i have this only running on posts, but you could easily mod it to suite whatever post type you’d like.
similar save function to store our tax data when a post is saved
again need to run something once to grab old posts, remove when done
add the menu this was the only part that wasn’t totally elegant, as it wasn’t straightforward to test whether a term had posts w/o the extra foreach loop. but i have mitigated that by storing it in a transient that only resets on post save.
so there are 2 solutions. the latter will get your URLs to say site.com/glossary/l without any htaccess rules to re-write query vars on your part. hope that helps.
I would suggest you to use
WP_Query
for this purpose. An example of this query is here in this plugin which fetches only relevant posts instead of using further PHP functions.You can choose a faster way to go on, Alphabetic Pagination Plugin