XenForo: How Do I Add Custom Fields to Thread List?

WendyMay

Well-Known Member
  • #1
I have a few custom fields, such as city and state, that I'd like to add to the thread list on the forum pages. Does anyone know how I might go about doing this? Is there template code that I need to use?
 

KristinaW

Active Member
  • #2
Okay, so I'm assuming you have these custom fields posted on the thread page itself and now you want them listed on the thread list on each individual forum page. Luckily, there's a fairly simply method for accomplishing this. What you need to do is open the thread_list_macros template.

Inside that template, you'll need to search for the place you want this custom field text to be placed. In the past, when I've done this sort of thing, I added the text after date that sits below the title. Search for this line (in version 2.2.2):

<li class="structItem-startDate"><a href="{{ link('threads', $thread) }}" rel="nofollow"><xf:date time="{$thread.post_date}" /></a></li>

Then, after that line, you'll want to add your custom code. Let's say that you have custom thread fields for city, state, and zip code. Let's pretend that these Field ID names for these are LocationCity, LocationState, and LocationZip respectively. The code you would add right after that line above is:

<!-- ADD CUSTOM FIELDS --> <xf:if is="{$thread.custom_fields.City}"><span>&#183;</span> {$thread.custom_fields.LocationCity},</xf:if> <xf:if is="{$thread.custom_fields.LocationState}">{$thread.custom_fields.LocationState}</xf:if> <xf:if is="{$thread.custom_fields.LocationZip}">{$thread.custom_fields.LocationZip}</xf:if> <!-- ADD CUSTOM FIELDS -->

I added a few more things to this as well, such as comments and a bullet point before the first custom field. Give this a try and let me know how that works.
 

WendyMay

Well-Known Member
  • #3
Thank you so much for this. One quick question. If I wanted to add the same thing to the Newest Threads widget, would you know how to do that as well?
 

KristinaW

Active Member
  • #4
Yes, you would add the same code to another place in the same template. The widget_newest_threads template actually pulls from the thread_list_macros template, so by updating the latter, you'll actually be updating the former.

You'll need to scroll down further in the same template until you reach the area the widget template pulls from. That section begins with:

<xf:macro name="item_new_threads" arg-thread="!">

In the past, I've posted the same code as I just showed you above right below the reply count code, which is:

<li>{{ phrase('replies:') }} {$thread.reply_count|number_short}</li>

So just search for that line and paste the custom code after it.
 

WendyMay

Well-Known Member
  • #5
Thank you so much. Okay, here's my dilemma now. One of my custom fields is a drop down selection for U.S. states. Most of the states are one word, but some are two, such as New York and South Carolina. When I use the:

<xf:if is="{$thread.custom_fields.LocationState}">{$thread.custom_fields.LocationState}</xf:if>

code, I get the result on the forum thread list like, "NewYork" and "SouthCarolina." See how the space is missing and these two words have been merged into one? The reason for this is because those one word values are field IDs for each state value. What I'd like to output is the resulting value that shows in the actual thread page, not the ID for the value. Any advice? By calling this:

{$thread.custom_fields.LocationState}

it's actually pulling the ID, not the resulting value. Or the visual value that the end user sees.
 

KristinaW

Active Member
  • #6
If you change your current line of template code from what you have to this:

<xf:if is="{$thread.custom_fields.LocationState}">{$thread.custom_fields.getFormattedValue('LocationState')}</xf:if>

you should be in good shape. What this says is, if there is a value for the state drop down, then output the formatted value that the end user sees. This is the major change here:

.getFormattedValue('LocationState')
 
Top