• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

How to show a post in a page

Status
Not open for further replies.

Fuhrmann

Well-known member
A user requested me to make a tutorial to show a post inside a XenForo page, so in this tutorial....I will teach you how to show a specific post in a XenForo page.

I get this example post:


1.webp



And turn into a page. like this:


2.webp



I strongly recommend you to read my others tutorials to know better how XenForo works:

How to show a forum in a page
How to read and write into the database (with a page)
How to create your own helpers
Creating a add-on to insert tabs in profile page (using hooks)
[part 2] Creating a add-on to insert tabs in profile page (using hooks)
[part 3] Creating a add-on to insert tabs in profile page (using hooks)
[part 4] Creating a add-on to insert tabs in profile page (using hooks)
How to create a cron entry to move threads (with options!)
How to use tabs to separate your add-on options
How to add a new sidebar in the forum list





This tutorial consists in 3 basic steps:


Step 1 - Creating the code
Step 2 - Creating the page
Step 3 - Testing



Here it is the final code. Just remember that this is not an addon, so there is no XML file. You need to folow the steps to create a new page.

http://dl.dropbox.com/u/5052517/Addons/De Tutoriais/ShowAPostInaPage.zip
 
Step 1 - Creating the code

Let's start by creating the code responsable to get the content of the post.

First thing you need to do is create a new file. You can place this file anywhere, but in this tutorial we will use a dedicated folder for it. Go to your_xenforo_root/library and create this directory structure:

--|library
------|ShowAPost *create this folder*
---------|View.php *create this file*
---------|Index.php *create this file*


The Index.php file will be the file that we will declare the callback class and method to our page. Copy and past the following code:

PHP:
<?php
/**
* How to show a post inside a page.
* This code have all the necessary to show a post inside a page. Just read the comments.
* Fuhrmann
*/
 
class ShowAPost_Index
{
    public static function showAPostInPage(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {   
        /* Set the ID of the post to be loaded */
        $postId = 180;
       
        /* Create a new ControllerHelper that will help us to get the post */
        $ftpHelper = new XenForo_ControllerHelper_ForumThreadPost($controller);
       
        /* Use the ControllerHelper to see if the post we want to get is viewable by the user browsing */
        list($post, $thread) = $ftpHelper->assertPostValidAndViewable($postId);
       
        /* If the post has attachments */
        if ($post['attach_count'] > 0)
        {
            /* Let's get all the attachments of this post, if exists  */
            $attachmentModel = XenForo_Model::create('XenForo_Model_Attachment');
            $attachments = $attachmentModel->getAttachmentsByContentId('post', $postId);
            foreach ($attachments AS $attachment)
            {
                /* Insert into the post data the attachments */
                $post['attachments'][$attachment['attachment_id']] = $attachmentModel->prepareAttachment($attachment);
            }
        }
       
        /* These parameters will be used in our template. We need to pass them into the response view. The $post param will be used too in the XenForo_ViewPublic instance */
        $viewParams = array (
            'post' => $post,
            'title' => $thread['title']
        );
       
        /* This way our post will be parsed and the aattachments too. :) */
        $response = $controller->responseView('ShowAPost_View', $response->params['templateTitle'], $viewParams);
       
        /* Return the response, finally!*/
        return $response;
    }
}

(The code is all explaned as comments. Please take a time to read all to understand better. The advanced stuff it is not so well explaned, because the objective of this tutorial is simple)

Do not forget to change the postId to what you want:

PHP:
$postId = 180;


Save!


Now, open the View.php file. This file will parse all bbcode in the post message.

Copy and paste inside the file:


PHP:
<?php
class ShowAPost_View extends XenForo_ViewPublic_Base
{
    public function renderHtml()
    {
        /* Get the post we need to parse the content */
        $post = $this->_params['post'];
     
        /* Create the parser to parse the bbcodes */
        $bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));
     
        /* Parse options */
        $bbCodeOptions = array(
            'states' => array(
                'viewAttachments' => true //If you don't want to show attachments, set this to false
            )
        );
     
        /* parse the post message and merge into the post data */
        $this->_params['post']['messageParsed'] = XenForo_ViewPublic_Helper_Message::getBbCodeWrapper($post, $bbCodeParser, $bbCodeOptions);     
    }
}
 
Step 2 - Creating the page

Now we'll create the page that will show the post you want to show. Go to AdminCP -> Applications -> Create new Page. In the next page, you have some fields to fill. Use the information below:

Basic Information:

  • Url Portion: showapost
  • Title: Show a Post (The title doest not matters, because we'll use the thread title)
  • Description: Show a post inside a page
Basic Information:
  • Template HTML:
    Code:
    <xen:h1>{$title}</xen:h1>
    {xen:raw $post.messageParsed}
PHP Callback:
  • PHP Callback: ShowAPost_Index::showAPostInPage
This is what you will have at the end:

5.webp 3.webp 4.webp



Save the page.
 
Step 3 - Testing

Now go to your forum list. You should see the page node there:

6.webp



Click to enter the page and see the result. Your post is now being displayed inside a page!

2.webp
 
Tips

  • If you want to show the page title and not the thread title, just change the Template HTMl of the page to this:
    Code:
    {xen:raw $post.messageParsed}
  • If you want to hide the breadcrumbs put the following code at the TOP of the template HTML of the page:
    Code:
    <style>
    .breadcrumb {
    display:none !important;
    }
    </style>
 
PHP:
    /* Use the ControllerHelper to see if the post we want to get is viewable by the user browsing */
        list($post, $thread) = $ftpHelper->assertPostValidAndViewable($postId);
 
        /* Let's get all the attachments of this post, if exists  */
        $attachmentModel = XenForo_Model::create('XenForo_Model_Attachment');
        $attachments = $attachmentModel->getAttachmentsByContentId('post', $postId);
        foreach ($attachments AS $attachment)
        {
            /* Insert into the post data the attachments */
            $post['attachments'][$attachment['attachment_id']] = $attachmentModel->prepareAttachment($attachment);
        }

PHP:
list($post, $thread) = $ftpHelper->assertPostValidAndViewable($postId);
if ($post['attach_count'])
{
// do the attach stuff ONLY if necessary;)
//so you're saving 1 db query.....
}

or if you want to show more posts, check postmodel::getAndMergeAttachmentsIntoPosts
 
Status
Not open for further replies.
Top Bottom