Search code examples
expressionengine

Output Custom Category Fields on a Zoo Triggers Listing


I have a Structure-based site with category listing pages. For example: http://www.mysite.com/products/category/tools/hammers. Naturally, I'm using Zoo Triggers to output the entry listing.

My problem is that each category listing page needs unique SEO meta data, so I set up custom fields for Title, Keywords, and Description. Unfortunately, Zoo Triggers doesn't have variables for custom category fields, and the {exp:channel:category_heading} tag won't work on Zoo Triggers URLs.

I thought I could use the magical GWCode Categories plugin, but I can't figure out how to display data for the current category only.

Does anyone have any suggestions on how I can make this happen? Thanks.


Solution

  • This probably calls for a custom query. As EE stores its core and custom category data in separate tables this is a bit unwieldy for the native query module, so I'd be inclined to use Rob Sanchez's ActiveRecord plugin. I haven't tested this extensively but the following snippet should be in the ballpark:

    {exp:activerecord
        select="d.field_id_1 as seo_field_one, d.field_id_2 as seo_field_two"
        from="category_field_data as d"
        join="categories as c"
        on="c.cat_id = d.cat_id"
        where[a]="c.cat_url_title = '{triggers:last_segment}'"
        where[b]="c.group_id = '1'"
    }
        <p>Field one: {seo_field_one}</p>
        <p>Field two: {seo_field_two}</p>
    {/exp:activerecord}
    

    If you're not particularly familiar with CodeIgniter's Active Record class, this is doing the following:

    • Selecting your custom category fields based on their column names, e.g. field_id_1, and creating aliases for use within your template, e.g. {seo_field_one}.
    • Creating a join between the categories and category_field_data tables so that we can retrieve data in the field_data table based on values in the categories table.
    • Restricting the results set to categories whose URL title matches the last URL segment and who belong to category group 1.

    In order to implement this in your template you'd need to adjust the select statement so that it's retrieving data from the right columns and tweak the group_id to match up with your category group. I'm assuming that you want to retrieve data for the last URL segment but you can easily adjust that too if need be.