Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

I'm having the same issue.

Can't get the widget to show only in particular specified NODES thru the widget interface.

I've tried...

$forum.node_id == 84
<xf:if is="$forum.node_id == 84">

and a variety of other "throw doo-doo on the wall" attempts.

Nothing seems to work.

...
I have tested:
<xf:if is="!in_array($forum.node_id, [84])">
YourCode here...
</xf:if>
and it work here for me in $forum.node_id 84 although he should not be executed here.
Without the "!" it wont be executed... very strange...
 
I have tested:
<xf:if is="!in_array($forum.node_id, [84])">
YourCode here...
</xf:if>
and it work here for me in $forum.node_id 84 although he should not be executed here.
Without the "!" it wont be executed... very strange...

Thanks but none of those worked. Node 84 is a category featuring 4 forums.

Tried entering this in the widget field...

<xf:if is="in_array($forum.node_id, [84])">

AND

is="in_array($forum.node_id, [84])

AND

in_array($forum.node_id, [84])

This is on top of

$forum.node_id == 84

<xf:if is="$forum.node_id == 84">


None of them work. The "new posts" widget just won't show per the specified node view.
 
Just an FYI what worked for me to show the LATEST POSTS Widget on a CATEGORY PAGE...

1) Enter development mode.
2) Create new widget position, category_top
3) Modify category_view template to include...

<xf:widgetpos id="category_view_top" context-category="{$category}" />

4) Create new widget in the new widget position.
5) Enter template syntax code to isolate category...

<xf:if is="$forum.node_id === 84">

Somewhat a tedious process, still wondering if there was an easier way that I've missed.


EDIT #1: CORRECTION: THIS DID NOT WORK.
ONCE AGAIN, THE WIDGET SYSTEM REFUSES TO HONOR ANY CODE RESPECTIVE TO NODE IDENTIFICATION.

EDIT #2:
This worked: $xf.reply.containerKey == 'node-16'

Replace the # with the node #..

More info: https://xenforo.com/community/threads/widget-display-condition.157920/
 
Last edited:
Surely one of these is wrong in the guide?

5. How can I Show content if not a member?

Code:
<xf:if is="!$xf.visitor.user_id">
Show content...
</xf:if>
6. How can I show different content to members and guests?

Code:
<xf:if is="!$xf.visitor.user_id">
Show only members
<xf:else />
Show only guests
</xf:if>
 
Surely one of these is wrong in the guide?

5. How can I Show content if not a member?

Code:
<xf:if is="!$xf.visitor.user_id">
Show content...
</xf:if>
6. How can I show different content to members and guests?

Code:
<xf:if is="!$xf.visitor.user_id">
Show only members
<xf:else />
Show only guests
</xf:if>

Yes, #6 is wrong. It's either:

Code:
<xf:if is="$xf.visitor.user_id">
Show only members
<xf:else />
Show only guests
</xf:if>

or

Code:
<xf:if is="!$xf.visitor.user_id">
Show only guests
<xf:else />
Show only member
</xf:if>
 
Code:
<xf:if is="!$xf.visitor.user_id AND in_array($forum.node_id, [10,12,15])">
I've already tried that code but it hides content from ALL threads except these in 10,12,15 forums from guests and all threads with no exception from registered members.
 
Code:
<xf:if is="!$xf.visitor.user_id AND in_array($forum.node_id, [10,12,15])">
I've already tried that code but it hides content from ALL threads except these in 10,12,15 forums from guests and all threads with no exception from registered members.

My mistake... "$__globals" sometimes is a musthave ;)
Code:
<xf:if is="$xf.visitor.user_id and in_array($__globals.forum.node_id, [10,12,15])">
    Nothing for guest in this Forums.. Exit this..
  <else if>
    Do this for other
</xf:if>
 
Last edited:
I'm looking to only display certain widgets on the thread view pages only when
  1. the sidebar is on the side rather than at the bottom and
  2. the page is long enough
For 1 I could use the viewport width in the conditional. How would I do that?
But for 2 when posts can vary in length, is there a way to get the actual displayed length of the body block when first rendered?
 
Hi,
I read through the guide, but it doesn't explain how to do one for displaying content matching the post's user.

Example if the posting user is Admin, display; Administrator
if the posting user is in VIP group, display VIP.

I know those are doable by default, I'm just using it as an example. How would I accomplish it in Conditional statements?

Thanks!
 
Thanks for this sharing as it really useful.

How to put more than one nodeid in this code

Code:
$xf.reply.containerKey == 'node-98'


Appreciate if someone could helps. Thanks.
 
How can I show code in just ONE MEDIA CATEGORY only?

(So that I can setup a paragraph of text for each media category so it isn't using the description in the media category page)
 
Top Bottom