Textpattern tips, tutorials and code snippets

Use list of categories as values for a custom field dropdown

I have a client who wants to add all kinds of image galleries to their site. I have set up one image category for each gallery, so in the future they can add a new category and then add their images to that category. Pretty basic stuff. However, each gallery is associated with a single page on their site, so the gallery is called with each individual article. The most logical thing to do would be to add a custom field that allows them to simply type in the category name of the gallery they want to appear.

But then I thought: why do it the easy way when I can do it the hard way?

So here is a quick and dirty solution for dynamically pulling in a list of specific categories to populate a custom field dropdown.

1. Download and install glz_custom_fields

Pretty self-explanatory. And pay gerhard; he worked hard on that thing.

2. Build a custom script

Gerhard supplies a well-documented custom script called my_images.php that can serve as a template for these types of things. All I did was change a few things around. So, copy and paste this script into a file called list_categories.php.

function list_categories($custom_field, $custom_id, $custom_value) {
    $categories = array();
    $query = safe_rows('name, title', 'txp_category','1=1');
    foreach ($query as $category)
        $categories[$category['name']] = $category['title'];
    return glz_selectInput($custom_field, $custom_id, $categories, $custom_value, "");

This will pull all categories. If you want to dictate a parent category, you need to add a WHERE clause to the safe_rows() function. In my case, I created a parent category called “galleries”, so I changed this:

$query = safe_rows('name, title', 'txp_category');

to this:

$query = safe_rows('name, title', 'txp_category', 'parent like "galleries"');

If you want to pull all categories for articles, images, links or files, you have to use the “type” column from the database, not the “parent” column. Example:

$query = safe_rows('name, title', 'txp_category', 'type like "image"');

That’s it. Pretty simple. Now copy list_categories.php to your server.

3. Add custom script to glz_custom_fields

Once glz_custom_fields is installed, go to Extensions > Custom Fields. Edit one of your un-used 1-10 custom fields, or choose to add a new one. Give it an appropriate name (in my case “Gallery”), and select “Custom Script” from the Type dropdown. In the Value field, add the complete path to the script, like so:


Click SAVE and you’re all set.

4. Put it to use

When you create or edit an article, you should see this dropdown appear with the appropriate categories. Remember, while the category titles are displaying, the category name (the one that counts) is the actual value for the option tag. So this is how I used it in my article form:

<txp:if_custom_field name="Gallery">
<ul id="photos" class="galleryview">
   <txp:smd_gallery form="gallery_item" category='<txp:custom_field name="Gallery" />' />

(In my case, smd_gallery is doing all the heavy lifting for the actual gallery building.)

Have fun.

More information in the forum thread.

1 Comment Comment feed

  • Tye
  • 14 December 2010

Wow – thanks Kevin – this is genius :)

Something I have been looking for for a while :)

Add a comment

Use Textile help to style your comments