Other How to show a post in a page - Cách hiển thị bài viết trong trang

PVS

Super Moderator
Thành viên BQT
Tham gia
28/02/2015
Bài viết
16,833
Được Like
12,688
How to show a post in a page - Cách hiển thị bài viết trong trang 1.0

Bước 1 - Tạo mã

Hãy bắt đầu bằng cách tạo ra các mã responsable để có được nội dung của bài viết.

Điều đầu tiên bạn cần làm là tạo ra một tập tin mới. Bạn có thể đặt tập tin này bất cứ nơi nào, nhưng trong bài này chúng ta sẽ sử dụng một thư mục dành riêng cho nó. Tới your_xenforo_root/library và tạo cấu trúc thư mục này:

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

Tập tin Index.php sẽ được các tập tin mà chúng ta khai báo các class gọi lại và phương pháp cho trang. Sao chép và dán đoạn mã sau đây:
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;
    }
}

Đừng quên thay đổi postID với những gì bạn muốn:
PHP:
$postId = 180;

Lưu lại.

Bây giờ, mở tập tin View.php. Tập tin này sẽ phân tích tất cả bbcode trong tin nhắn bài viết.

Sao chép và dán vào tập tin:
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);
    }
}

Bước 2 - Tạo trang

Bây giờ chúng ta sẽ tạo ra các trang sẽ hiển thị các bài viết mà bạn muốn hiển thị. Tới AdminCP -> Applications -> Create new Page. Trong trang tiếp theo, bạn có một số lĩnh vực để điền vào. Sử dụng các thông tin dưới đây:

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:
    Mã:
    <xen:h1>{$title}</xen:h1>
    {xen:raw $post.messageParsed}
PHP Callback:
  • PHP Callback: ShowAPost_Index::showAPostInPage
Đây là những gì bạn sẽ có ở cuối:

3.png



4.PNG


5.PNG

Lưu trang.

Bước 3 - Kiểm tra

Bây giờ vào danh sách diễn đàn của bạn. Bạn sẽ thấy node trang có:

6.PNG

Click để vào trang và xem kết quả. Bài viết của bạn hiện đang được hiển thị bên trong một trang!

2.PNG

Mẹo

Nếu bạn muốn hiển thị tiêu đề trang và không phải là tiều đề của chủ đề, chỉ cần thay đổi template HTML của trang này:
Mã:
{xen:raw $post.messageParsed}

Nếu bạn muốn ẩn breadcrumbs thì hãy đặt mã sau vào đầu template HTML của trang:
Mã:
<style>
.breadcrumb {
display:none !important;
}
</style>

1.PNG

Chúc các bạn thành công.


Nguồn: xenforo.com​
 

Đính kèm

  • ShowAPostInaPage.zip
    1.8 KB · Lượt xem: 1

Hướng dẫn sử dụng

XenForo 1 XenForo 2
Translate by PVS

Dịch vụ XenForo của VNXF

Mr. Tuấn

Mobile/Zalo: 0988 488 096

Telegram: bluekpro

Email: [email protected]

Nhà Tài Trợ

Mút Xốp Không Gian
pallet Thịnh Phát
Top Bottom