XF 2.1 Conditional in templates

Lee

Well-known member
I am looking to build a conditional to manipulate the meta data in threads using a custom thread field.

I have a field called "meta description" which is only editable by admin.

I then want to use this field to replace the meta description when the appropriate threads are loaded.

Is this possible using template conditionals?

I was thinking of something like:

If user is viewing a thread
Use custom meta description field
Else
Use regular meta description
 
Easiest down and dirty way of doing it.

Ensure that the display field is "before message" in ACP / custom thread fields as the text you input will be visible to everyone browsing and you only intend on making it visible as a meta/description. As there's no custom/self-placement for custom thread fields, yet, we need to hide this so it's important that it's set to "before" to hide it with CSS or we'll need to do another template edit (seen above if you really want to, but like the message if you agree that there should be self-placement).

Next, open the thread_view template.

Find:
Code:
<xf:set var="$fpSnippet" value="{{ snippet($firstPost.message, 0, {'stripBbCode': true}) }}" />
Replace with:
Code:
<xf:comment>Check if there's none set. If none, use default; if set, use set snippet.</xf:comment>
<xf:if is="!$thread.custom_fields.customMetaDesc">
    <xf:comment>Use the default thread snippet if one is not set</xf:comment>
    <xf:set var="$fpSnippet" value="{{ snippet($firstPost.message, 0, {'stripBbCode': true}) }}" />
<xf:else />
    <xf:comment>Use the snippet an admin set</xf:comment>
    <xf:set var="$fpSnippet" value="{{ snippet($thread.custom_fields.customMetaDesc, 0, {'stripBbCode': true}) }}" />
</xf:if>
<xf:css>
    <xf:comment>Hide the visible custom thread field without another template edit.</xf:comment>
    .message-fields--before { display: none !important; }
</xf:css>

It's pretty self-explanatory with the <xf:comment> remarks above.

The only thing you need to look for is $thread.custom_fields.customMetaDesc and replace the customMetaDesc portion of that with your own custom thread Field ID (as seen below):

1581611889032.png
 
Easiest down and dirty way of doing it.

Ensure that the display field is "before message" in ACP / custom thread fields as the text you input will be visible to everyone browsing and you only intend on making it visible as a meta/description. As there's no custom/self-placement for custom thread fields, yet, we need to hide this so it's important that it's set to "before" to hide it with CSS or we'll need to do another template edit (seen above if you really want to, but like the message if you agree that there should be self-placement).

Next, open the thread_view template.

Find:
Code:
<xf:set var="$fpSnippet" value="{{ snippet($firstPost.message, 0, {'stripBbCode': true}) }}" />
Replace with:
Code:
<xf:comment>Check if there's none set. If none, use default; if set, use set snippet.</xf:comment>
<xf:if is="!$thread.custom_fields.customMetaDesc">
    <xf:comment>Use the default thread snippet if one is not set</xf:comment>
    <xf:set var="$fpSnippet" value="{{ snippet($firstPost.message, 0, {'stripBbCode': true}) }}" />
<xf:else />
    <xf:comment>Use the snippet an admin set</xf:comment>
    <xf:set var="$fpSnippet" value="{{ snippet($thread.custom_fields.customMetaDesc, 0, {'stripBbCode': true}) }}" />
</xf:if>
<xf:css>
    <xf:comment>Hide the visible custom thread field without another template edit.</xf:comment>
    .message-fields--before { display: none !important; }
</xf:css>

It's pretty self-explanatory with the <xf:comment> remarks above.

The only thing you need to look for is $thread.custom_fields.customMetaDesc and replace the customMetaDesc portion of that with your own custom thread Field ID (as seen below):

View attachment 218377
Thank you, I will try this and report back.
 
Top Bottom