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

Need help with something

Discussion in 'XenForo Development Discussions' started by Matthew Hawley, Jun 13, 2013.

  1. Matthew Hawley

    Matthew Hawley Well-Known Member

    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!
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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>
     
    LPH, Jon W, Brogan and 1 other person like this.
  3. Matthew Hawley

    Matthew Hawley Well-Known Member

    Thanks so much Chris! Your a life saver! :)
     
  4. Jon W

    Jon W Well-Known Member

    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 likes this.
  5. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  6. Matthew Hawley

    Matthew Hawley Well-Known Member

    So, how do I apt he query and database thing?
     
  7. Jon W

    Jon W Well-Known Member

    Is a lot easier to do it in the template though!
     
  8. Matthew Hawley

    Matthew Hawley Well-Known Member

    But you said it wouldn't work for all pages.
     
  9. James

    James Well-Known Member

    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.
     
    Matthew Hawley likes this.
  10. Jon W

    Jon W Well-Known Member

    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.
     
    James likes this.

Share This Page