Ask and ye shall receive!
I have been going through the backlog of comments and noted that a couple of people wanted to sort their indexes in chronological order — i.e. they want the list of posts sorted by date with the most recent at the top. So I was just pondering the issue, wondering if I should really add this feature even though it’s not really something AZIndex is designed to do, when I suddenly had “D’oh” moment…
You can already do this with the current release of AZIndex! (By using a one-line custom compare function.)
Here’s how you do it:
- Go to the AZIndex management page (Manage >> AZIndexes) and create a new index.
- Set up your settings as usual, but also select the advanced option “Use a customized comparison function for sorting the index“.
- A new option field will appear where you can type in the name of the custom compare function you will be adding. Type in “az_chronological_compare“.
- Save your changes.
- Now we need to add the compare function somewhere, and a good place for that is in the functions.php file of your current theme. Go to Design >> Theme Editor.
- Look for a file called something like Theme Functions (functions.php) in the list on the right and click on the link to load it into the editor.
- Add the following function somewhere in the file:
function az_chronological_compare($in1, $in2) {
return $in1['id'] < $in2['id'];
}IMPORTANT: The function must be between PHP start and end tags: i.e. somewhere between <?php and ?>. If they don’t exist in the file, simply add them around the function.
- Click Update File at the bottom of the page to save your changes.
- Now go to the page created when you created the index and take a look. The items in the index should appear in chronological order starting with the most recent. (If you want the oldest first, simple change the ‘<‘ for a ‘>‘.)
There are a few important things to consider about this solution:
- This method works because WordPress increments the post and page IDs by one every time a new post or page is created. If, for any reason, the incremented ID was reset to a lower value then the posts will not appear in the expected order. It is highly unlikely that has happened on your blog (probably a far less than 1% chance) because it is never done by WordPress itself, so in all probability this method will work just fine.
- There is absolutely no point in using the alphabetical headings or links options with this type of index. In fact, the plugin will probably get hopelessly confused and generate some very strange looking output! If you avoid using those options, you should be fine.
- I am not planning on adding any new features for chronological indexes (like month or year headings, etc). I suspect there are other ways or other plugins that already do that type of archiving already.
I get an error: “Parse error: syntax error, unexpected ‘}’ in /customers/equil.net/equil.net/httpd.www/cineasten/wp-content/themes/sleepy-blue-10/functions.php on line 5”
The error seems to stick, even when line 5 is blank, and regardless of where I place the “}” (or even if I remove it).
Sorry, got it to work by putting the “}” right after the “$in2[‘id’];”, without using up another line. Great!
The index puts the newest posts on top, and the oldest post in the bottom. Is it possible to reverse the order in some simple manner?
Huh — I don’t know why you would get an error putting the ‘}’ on a new line. Perhaps an illegal invisible character got stuck in the middle somewhere.
Anyway, glad you got it to work. As for putting the oldest first, just replace the ” to sort the posts in the opposite order.
Hello Mike, I found your GREAT PLUGIN but how can I use your plugin in every category? (Because I have around 700 posts)
Hi Edie — if you want to make an index for all of your 700 posts in your blog then just leave the “Included categories” option field blank in the settings for your index.
If I have misunderstood the question, please let me know.
Thanks for fast reply! But my description was bad.
I want to have your a-z index in every category (not index off all posts). In the first category (50 posts), second one (70 posts). Is any simple way how to do this?
Just 700 posts? :p I have around 1500, and I write some 3-4-5 posts every day for my movieblog. Needless to say, my index is somewhat crowded 😉
Edie, there is not a way to do this with one index, but you can create as many indexes as you want so you can create one index per category, and then you can create another index which just contains the other index pages. In other words, you would be creating a top-level index, or an index of indexes.
For example, if you have four categories — books, movies, music, television — you can create one index for each category, and you will also have created four new pages with those indexes on them. Next you can tag those index pages with a unique tag — “indexes” — and then create another index for all your other four index pages (select the “pages only” option in that index).
Note: if you like this idea, then you will have to install a plugin that will allow you to add tags to blog pages. I have used “tags4page” in the past — http://wordpress.org/extend/plugins/tags4page/ — which is simple to use. (There may be other plugins that do the same thing).
You can, of course, just create a static blog page which contains links to all your indexes, but the advantages of creating a top-level index is that when you add a new category and a new index, it will automatically be added to your top-level index (if you remember to tag it correctly) so there would be no need to edit the top-level index page.
If you only have a small number of categories — up to 20? — then that should be a workable solution. If you have many more than that, it would probably take a long time to set up (though you should only have to do it once). If you like the idea, test it out with a small number of indexes first to see if you like the results.
Let me know what you think.
EM
Yes, I agree I stardet creating indexes, it is arround 20. It´s ok. Thanks for your time.
No problem. I hope you like the results!
I hope my last message on this post. I tried to create my indexes:
But is shows me this mistake:
Parse error: syntax error, unexpected $end in…
🙁
There is code:
It does not show code, so I placed it here: http://podpora.dgx.cz/wordpress/viewtopic.php?pid=11494#p11494
Hey Edie — sorry it took so long to get back to you. What you did wasn’t exactly what I was suggesting. You created indexes for each of your categories, which is correct, but then you should not have to write any code to create another page for the top-level index. Why not just let AZIndex do it for you?
Let me explain.
When you created all those indexes (1-22) AZIndex also created a page for each of them (they will be created “unpublished”). Create one more index and set it to include “Pages Only” which have the tag “index” (or something similar).
Now, to add the “index” to the 22 index pages, you will have to install the plugin “tags4page” — http://wordpress.org/extend/plugins/tags4page/ — since WordPress doesn’t give you a way to tag pages (but they are supported once you add them). And so your top-level index is an index of other index pages. You should not have to write any code at all.
This may be a little work to set it up, but if you ever add a new category, all you have to do is create a new index for that category and add the “index” tag to the new index’s blog page and it will be added to the top-level index automatically.
Note: if you don’t want your index pages appearing in a list of pages (which some themes put on the home page) then make the top-level index page the parent page for all the other pages.
Edie,
I should have commented on the code you posted — sorry about that.
It looks as though you are trying to put all the indexes on the same page — at least I believe that’s what the code looks like it would do if you were not getting the syntax error. You can do this without any code — just by adding the short codes on after the other:
[az-index id=”1″]
[az-index id=”2″]
[az-index id=”3″] and so on.
But don’t think that is what you want. In any case, you should not put that code in a the post_loop — I don’t think that has any chance of working the way you want it to. My best advice is to follow the instructions I gave above, and then all you will need in your blog index page is
[az-index id=”23″] (or whatever the top-level index id is).
EM