How to Show a List of all Possible Values in a Custom Field in Movable Type

| 2 Comments | No TrackBacks |
User birdahonk on the Movable Type forums was looking for a way to display a list of the countries in which the pictures on his photoblog were taken.  The country of each photo was stored in a custom field, and he would like to have a simple list of them, without duplicates.  Some Movable Type template magic to the rescue!
Here is my solution:

<mt:entries lastn="9999">
<mt:entrydatacountry setvar="country">
<mt:var name="countries{$country}" value="1">
</mt:entries>

<ul>
<mt:loop name="countries" sort_by="key">
<mt:if name="__key__"><li><mt:var name="__key__"></li></mt:if>
</mt:loop>
</ul>

First it loops over all entries, storing the country as the key in a hash.  This gets rid of duplicates.  Then it loops over the hash, displaying the keys (ignoring the empty key, if there was an entry with no country set).

If the blog is very large, I would recommend setting up this snippet of template code as a module with caching turned on and processed as a server side include.  This module could then be included either directly or in a separate index template, the output of which could then be included via a 'manual' server side include as well.  The advantage of using a dedicated index template for this is that it could be set to use background publishing, which would improve backend performance even more on a large blog.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/1468

2 Comments

Thanks, Maarten

This worked perfectly! I was able to easily replicate for my "Cities" custom field as well. Very nice, thanks again for jumping in and helping. It looks great.

I'll come back and post a link to the site once it launches.

Brian (birdahonk)

Leave a comment