• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Need help with something

Matthew Hawley

Well-known member
#1
So I'm going to try to create an addon. But that's not what I need help with.

So I want to put a link in the thread template and I only want the link to show if the user has posted in the thread.

Could someone help me accomplish this? I would really appreciate it.

Thanks!
 

Chris D

XenForo developer
Staff member
#2
The thread_view template contains this code:

Code:
        <xen:foreach loop="$posts" value="$post">
            <xen:if is="{$post.message_state} == 'deleted'">
                <xen:include template="post_deleted_placeholder" />
            <xen:else />
                <xen:include template="post" />
            </xen:if>
        </xen:foreach>
This loops through the post records and basically renders each post.

Change it to this:

Code:
        <xen:foreach loop="$posts" value="$post">
            <xen:if is="{$post.message_state} == 'deleted'">
                <xen:include template="post_deleted_placeholder" />
            <xen:else />
                <xen:include template="post" />
            </xen:if>
            <xen:if is="{$post.user_id} == {$visitor.user_id}">
                <xen:set var="$hasPosted">1</xen:set>
            </xen:if>
        </xen:foreach>
Once there is a post by a user ID that matches the visitor's user ID, the {$hasPosted} variable is set to 1.

If there is no post by the current visitor in the thread the variable is NULL.

This means you can use:

Code:
<xen:if is="{$hasPosted}">
        VISITOR HAS POSTED IN THIS THREAD
<xen:else />
        VISITOR HAS NOT POSTED IN THIS THREAD
</xen:if>
 

Matthew Hawley

Well-known member
#3
The thread_view template contains this code:

Code:
        <xen:foreach loop="$posts" value="$post">
            <xen:if is="{$post.message_state} == 'deleted'">
                <xen:include template="post_deleted_placeholder" />
            <xen:else />
                <xen:include template="post" />
            </xen:if>
        </xen:foreach>
This loops through the post records and basically renders each post.

Change it to this:

Code:
        <xen:foreach loop="$posts" value="$post">
            <xen:if is="{$post.message_state} == 'deleted'">
                <xen:include template="post_deleted_placeholder" />
            <xen:else />
                <xen:include template="post" />
            </xen:if>
            <xen:if is="{$post.user_id} == {$visitor.user_id}">
                <xen:set var="$hasPosted">1</xen:set>
            </xen:if>
        </xen:foreach>
Once there is a post by a user ID that matches the visitor's user ID, the {$hasPosted} variable is set to 1.

If there is no post by the current visitor in the thread the variable is NULL.

This means you can use:

Code:
<xen:if is="{$hasPosted}">
        VISITOR HAS POSTED IN THIS THREAD
<xen:else />
        VISITOR HAS NOT POSTED IN THIS THREAD
</xen:if>
Thanks so much Chris! Your a life saver! :)
 

Jon W

Well-known member
#4
If you want to check if the user has posted on any other page in that thread, you will need to create an add-on to query the database and find out if the user has posted. I suggest querying the xf_thread_user_post table. You can then make a similar $hasPosted variable available to the template and add a conditional as Chris has suggested above.
 

Chris D

XenForo developer
Staff member
#5
Yeah, doing it entirely in the template isn't ideal.

That process will work on every single page in the thread, but it will only tell you if that user has posted on that page of the thread, which may not be entirely what you want.
 

Matthew Hawley

Well-known member
#6
If you want to check if the user has posted on any other page in that thread, you will need to create an add-on to query the database and find out if the user has posted. I suggest querying the xf_thread_user_post table. You can then make a similar $hasPosted variable available to the template and add a conditional as Chris has suggested above.
So, how do I apt he query and database thing?
 

James

Well-known member
#9
Yeah, doing it entirely in the template isn't ideal.

That process will work on every single page in the thread, but it will only tell you if that user has posted on that page of the thread, which may not be entirely what you want.
I think he wants to just see if a user has posted at all in the thread, so that method should work fine in the templates.
 

Jon W

Well-known member
#10
As long as the user goes to one of the pages they have posted on, they will be able to see the link. If that's enough for you, then the template method is fine.

If you need to show the link on every page of the thread regardless of which page they have posted on, you will have to build an add-on. There is no other way.