XF 2.1 Show Custom Thread Fields in forum view

ibaker

Well-known member
I have created a classifieds section using a forum for Classified entries. I have also created 6 different Custom Thread Fields with 3 of them being Price, Condition and Location.

Can anyone help me to be able to display those 3 custom fields for each entry in the forum view.

This is what I need:
1.jpg

I would be grateful for any help...thanks
 

Kevin

Well-known member
I'm working on some stuff with custom thread fields and will be doing similar of displaying some values in the forum listings.

You can play around with your HTML & CSS for how you want the value to look but at it's simplest you'll want to modify your thread_list_macros template and then display your variables using {$thread.custom_fields.NAME} where NAME is the name of your custom thread field (so if you named it "price" it'd be {$thread.custom_fields.price} and so on).

To avoid any errant output output you'll also like want to wrap it inside of a conditional to only display if the custom thread field actually has a value. Using "price" as an example it'd be <xf:if is="$thread.custom_fields.price">{$thread.custom_fields.price}</xf:if>.

For the spot on your mockup with the red square, on an unmodified copy of the thread_list_macros template try this at line 165 (again, assuming that "price" is a name of a custom thread value you have along with "location" and "condition"), just update the "myDivClass" and "mySpanClass" class names as needed.

Before...
Code:
            </div>
        </div>
        <div class="structItem-cell structItem-cell--meta" title="{{ phrase('first_message_reaction_score:')|for_attr }} {$thread.first_post_reaction_score|number}">
After...
Code:
            </div>
            <div class="MyDivClass">
                <ul class="structItem-parts">
                <xf:if is="$thread.custom_fields.price">
                    <li class="mySpanClass">Date: {$thread.custom_fields.price}</li>
                </xf:if>
                <xf:if is="$thread.custom_fields.location">
                    <li class="mySpanClass">{$thread.custom_fields.location}</li>
                </xf:if>
                <xf:if is="$thread.custom_fields.condition">
                    <li class="mySpanClass">{$thread.custom_fields.condition}</li>
                </xf:if>
                </ul>
            </div>
        </div>
        <div class="structItem-cell structItem-cell--meta" title="{{ phrase('first_message_reaction_score:')|for_attr }} {$thread.first_post_reaction_score|number}">
 

ibaker

Well-known member
@Kevin thanks so much worked really well except for 1 small issue which I think to fix would cause a performance hit...if you have a choice field for a Custom Thread Field the "value" is displayed not the "text". This means if you have for example 2 words like "needs repair" you can only use a-z, A-Z, 0-9, and _ characters so Needs Repair would be displayed with an underscore i.e. Needs_Repair.

Don't know if you need to also consider this for your own project

Also I used this css formatting:
Code:
            <div class="structItem-minor">
                <ul class="structItem-parts">
                <xf:if is="$thread.custom_fields.price">
                    <li>Price: {$thread.custom_fields.price}</li>
                </xf:if>
                <xf:if is="$thread.custom_fields.condition">
                    <li>{$thread.custom_fields.condition}</li>
                </xf:if>
                <xf:if is="$thread.custom_fields.location">
                    <li>{$thread.custom_fields.location}</li>
                </xf:if>
                </ul>
            </div>

2.jpg
 
Last edited:

ibaker

Well-known member
@Kevin if interested I fixed the choice field issue by using {$thread.custom_fields.getFormattedValue('condition')} plus in my situation I have a field Location so I have wrapped the value in a google map link:

Code:
<div class="structItem-minor">
    <ul class="structItem-parts">
        <xf:if is="$thread.custom_fields.price">
            <li>Price: {$thread.custom_fields.price}</li>
        </xf:if>
        <xf:if is="$thread.custom_fields.condition">
            <li>{$thread.custom_fields.getFormattedValue('condition')}</li>
        </xf:if>
        <xf:if is="$thread.custom_fields.location">
            <li><a href="{{ link('misc/location-info', '', {'location': $thread.custom_fields.location}) }}" rel="nofollow noreferrer" target="_blank" class="u-concealed">{$thread.custom_fields.location}</a></li>
        </xf:if>
    </ul>
</div>
 

Kevin

Well-known member
Glad to see you got it working the way you wanted! :cool:

Thanks for following-up with what was needed to get the values for the selectable fields. (y) At the moment my current weekend project with custom thread fields (CTF) is only straight input values & dates but at some point I will need to do exactly what you're doing as well. At the moment I'm using a 3rd-party addon for our "Classifieds" forum since that was the easiest way of migrating that site from XF1 to XF2 but when the day comes that an XF upgrade breaks the add-on then I'll be switching over to using just CTFs (or if/when I add "classifieds" to some of the other sites in the collection).

CTFs have a lot of potential, I'm hoping they are given some love with expanded options/features in future XF updates.
 

gogo

Active member
Is this a typo? I need to delete it in order to have the layout displayed properly.

1591152704144.png

It works otherwise. Thanks a lot for the information.
 
Last edited:

gogo

Active member
@Kevin if interested I fixed the choice field issue by using {$thread.custom_fields.getFormattedValue('condition')} plus in my situation I have a field Location so I have wrapped the value in a google map link:

Code:
<div class="structItem-minor">
    <ul class="structItem-parts">
        <xf:if is="$thread.custom_fields.price">
            <li>Price: {$thread.custom_fields.price}</li>
        </xf:if>
        <xf:if is="$thread.custom_fields.condition">
            <li>{$thread.custom_fields.getFormattedValue('condition')}</li>
        </xf:if>
        <xf:if is="$thread.custom_fields.location">
            <li><a href="{{ link('misc/location-info', '', {'location': $thread.custom_fields.location}) }}" rel="nofollow noreferrer" target="_blank" class="u-concealed">{$thread.custom_fields.location}</a></li>
        </xf:if>
    </ul>
</div>

Your follow up is important. Thanks.
 

Anatoliy

Well-known member
To avoid any errant output output you'll also like want to wrap it inside of a conditional to only display if the custom thread field actually has a value. Using "price" as an example it'd be <xf:if is="$thread.custom_fields.price">{$thread.custom_fields.price}</xf:if>.

I follow your advice. Inserted <xf:if is="$thread.custom_fields.amp">{$thread.custom_fields.amp}</xf:if> after <head> in the PAGE_CONTAINER template. It output custom field data, however not in the head section but in the body.
Would you please advice what's going on and how to fix it? Thanks in advance.
 
@Kevin , in reference to your post earlier in this thread, I have been using your tip for quite some time and it works great! Would you happen to know how I can make the thread fields recognize the line breaks? Here's what I'm trying to do and can't figure it out.

NOTE: When the custom thread field is typed, the line breaks display as they should, however, in forum view, the line breaks aren't recognized.

testpostitle.png
 
Top