Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

How do I show content if a user is a member of a certain usergroup? I want to extend the user's postbit with designs. But whenever I do

Code:
<xf:if is="{{$xf.visitor.isMemberOf(x)}}">

    Show content...

</xf:if>

It will only show content if the logged in user is the one specified in the code. I kinda want to give certain usergroups a CSS class so I can configure them per usergroup.
 
Is it possible to show content only if page URL equals to something? For example to show the login form only if its the login page (/login/)
I cannot use template = login because the login must be implemented IN the login template.

Unless I create a template modification with a callback which will check in PHP if the page is the login. But it sounds like a bad solution. Or?
 
Is it possible to show content only if page URL equals to something? For example to show the login form only if its the login page (/login/)
I cannot use template = login because the login must be implemented IN the login template.

Unless I create a template modification with a callback which will check in PHP if the page is the login. But it sounds like a bad solution. Or?

You could try:
HTML:
<xf:if is="$xf.uri == '/login/'">
    Your content
</xf:if>
 
I used the following in XF1.5 so Google Ad Manager could pull page codes into GAM for ad targeting. What updates do these need to work? Thank you!

googletag.pubads().setTargeting('VisitorID', '{$visitor.user_id}');
googletag.pubads().setTargeting('Template', '{$contentTemplate}');
googletag.pubads().setTargeting('ForumID', '{$forum.node_id}');
googletag.pubads().setTargeting('ParentID', '{$forum.parent_node_id}');
 
14. How can I hide content from more than one user group?

Code:
<xf:if is="{{!$xf.visitor.isMemberOf([x, y])}}">
Hide content...
</xf:if>


This isn't working for me for some reason.... x & y= the number next to the user group url in admin tool?
 
googletag.pubads().setTargeting('VisitorID', '{$xf.visitor.user_id}'); <-- updated
googletag.pubads().setTargeting('Template', '{$template}'); <-- updated
googletag.pubads().setTargeting('ForumID', '{$forum.node_id}'); <-- same as XF1
googletag.pubads().setTargeting('ParentID', '{$forum.parent_node_id}'); <-- HELP

I've tried a handful of possible options, none successful at returning a value....ANYONE know what this variable is?
 
This is called in the header as part of the ad serving code. Obviously, this variable only returns a value on specific pages/sections, but I've used it successfully for a couple years with XF1.5 to target direct ad campaigns in GAM to specific sections of the site. The other 3 variables return values, I simply need to know this 4th variable.
 
Last edited:
Great guide.

Bit of a typo in # 44? No curly braces and no !in_array ?

Should this:
<xf:if is="in_array($forum.node_id, [x,y,z])">

Be this:
<xf:if is="!in_array({$forum.node_id}, [x,y,z])">



43. How can I show content in more than one forum?
Code:
<xf:if is="in_array({$forum.node_id}, [X,Y,Z])">
Show content..
</xf:if>
44. How do I hide content in multiple forums?
Code:
<xf:if is="in_array($forum.node_id, [x,y,z])">
Hide content..
</xf:if>
 
I used the following in XF1.5 so Google Ad Manager could pull page codes into GAM for ad targeting. What updates do these need to work? Thank you!

googletag.pubads().setTargeting('VisitorID', '{$visitor.user_id}');
googletag.pubads().setTargeting('Template', '{$contentTemplate}');
googletag.pubads().setTargeting('ForumID', '{$forum.node_id}');
googletag.pubads().setTargeting('ParentID', '{$forum.parent_node_id}');
Exactly my issue.
 
This doesn't work for me at all:

Code:
<xf:if is="{{$xf.visitor.isMemberOf(51)}}">

It ends up showing the content for all users regardless of what usergroup id I put. Any ideas?
 
$xf.visitor doesn't work, so I ended up using this and it works for me:

Code:
<xf:if is="{{$user.isMemberOf(51)}}">
and
<xf:if is="{$user.Profile.custom_fields.test}">
and
<xf:if is="$user.is_moderator">
and 
<xf:if is="$user.is_admin">
and 
<xf:if is="!$user.is_admin">

Just in case someone needs these for future reference.
 
Update to my previous post:

I was using this:

Code:
<xf:if is="{{$user.isMemberOf(x)}}">

But then when I went to a thread where some of the posts were from guests (old accounts that were deleted so now it shows them as "guest"), I would get an error:

Code:
ErrorException: Template error: Cannot call method isMemberOf on a non-object (NULL)
src/XF/Template/Templater.php:907

So then I changed it to this:

Code:
<xf:if is="$user AND {{$user.isMemberOf(x)}}">
code here
</xf:if>

And it's fine.
 
Looking for conditional statement to hide a particular javascript code (google auto ads) from one specific forum (node 116) and all threads inside it.
The code is added to page container template and it does not seem to be working for me. I am using this snippet.

Code:
<xf:if is="$forum.node_id != 3">
               CONTENT
</xf:if>

192426

Is this supposed to work in xenforo templates? Is this the code that would hide CONTENT from forum 116 and threads in forum 116 but keep it working elsewhere? Or should I move the code to Container Header in Advertising section in backend?
 
Top Bottom