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

How to display latest posts externally from xF?

#1
I'm trying to add a small side bar addition to my site's index, the problem is the index page is managed by Drupal, and not xenForo. I did find this tread, but it had been archived and I'm not sure if it would still work with the most recent version of xF. Ideally, I'd like it to have a similar output to this:

Any suggestions would be greatly appreciated.
 

Brogan

XenForo moderator
Staff member
#2
This forum is for development support - do you know how to write PHP?
If so, you can use that thread as a reference to write the code.

If not, you should be posting in the custom request forum.
 
#3
I know a fair amount of PHP. I just wanted to make sure that something like that archived thread could still be done and be compatible. Sorry if I put this in the wrong spot, I used a similar topic as reference.
 

Brogan

XenForo moderator
Staff member
#4
The code in that thread should still generally be OK (I haven't looked at in detail though).

Essentially you just need to query the database based on your desired criteria and format the output in a template.
 

LPH

Well-known member
#6
PHP:
// set the amount of posts to display
$limit = 5;

$startTime = microtime(true);

// Set to forum location - See XenForo index.php for information
$fileDir = dirname(__FILE__);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

$forumModel = XenForo_Model::create('XenForo_Model_Forum');

$visitor = XenForo_Visitor::getInstance();
$permissionCombinationId = $visitor['permission_combination_id'];

$threads = $forumModel->getThreads(array('discussion_state' => 'visible', 'permissionCombinationId' => $permissionCombinationId), array('limit' => $limit, 'order' => 'thread_id', 'orderDirection' => 'desc') );

foreach ( $threads AS $thread ) {
   
    $visitor->setNodePermissions( $thread['node_id'],
        $thread['node_permission_cache'] );
    if ( $forumModel->canViewForum( $thread ) ) {
       
        echo(
            "<div class='entry-meta'><a href='"
            . XenForo_Link::buildPublicLink( 'canonical:threads',
                $thread )
            . "'>"
            . XenForo_Helper_String::wholeWordTrim( $thread['title'],
                40 )
            . "</a></div>"
        );
    }
}
 

Marcus

Well-known member
#7
The most easy way might be just putting a javascript box on your external page that receives the RSS feed from the forum.
 

LPH

Well-known member
#9
Sorry - I cannot edit the code that was posted. It is wrong though. It contains XenForo_Model_Forum when it should be XenForo_Model_Thread. This code is better.

Thank you @AndyB.

PHP:
<?php

// set the amount of posts to display
$limit = 5;

$startTime = microtime( true );

// Set to forum location - See XenForo index.php for information
$fileDir = '/Applications/MAMP/htdocs/community';

require( $fileDir . '/library/XenForo/Autoloader.php' );
XenForo_Autoloader::getInstance()->setupAutoloader( $fileDir . '/library' );

XenForo_Application::initialize( $fileDir . '/library', $fileDir );
XenForo_Application::set( 'page_start_time', $startTime );

$threadModel = XenForo_Model::create( 'XenForo_Model_Thread' );

$threads = $threadModel->getModelFromCache('XenForo_Model_Thread')->getThreads( array(
    'discussion_state'        => 'visible',
), array(
    'limit'          => $limit,
    'order'          => 'thread_id',
    'orderDirection' => 'desc'
) );

foreach ( $threads AS $thread ) {

        echo(
            "<div class='entry-meta'><a href='"
            . XenForo_Link::buildPublicLink( 'canonical:threads',
                $thread )
            . "'>"
            . XenForo_Helper_String::wholeWordTrim( $thread['title'],
                40 )
            . "</a></div>"
        );
}
 

|Jordan|

Active member
#10
Sorry - I cannot edit the code that was posted. It is wrong though. It contains XenForo_Model_Forum when it should be XenForo_Model_Thread. This code is better.

Thank you @AndyB.

PHP:
<?php

// set the amount of posts to display
$limit = 5;

$startTime = microtime( true );

// Set to forum location - See XenForo index.php for information
$fileDir = '/Applications/MAMP/htdocs/community';

require( $fileDir . '/library/XenForo/Autoloader.php' );
XenForo_Autoloader::getInstance()->setupAutoloader( $fileDir . '/library' );

XenForo_Application::initialize( $fileDir . '/library', $fileDir );
XenForo_Application::set( 'page_start_time', $startTime );

$threadModel = XenForo_Model::create( 'XenForo_Model_Thread' );

$threads = $threadModel->getModelFromCache('XenForo_Model_Thread')->getThreads( array(
    'discussion_state'        => 'visible',
), array(
    'limit'          => $limit,
    'order'          => 'thread_id',
    'orderDirection' => 'desc'
) );

foreach ( $threads AS $thread ) {

        echo(
            "<div class='entry-meta'><a href='"
            . XenForo_Link::buildPublicLink( 'canonical:threads',
                $thread )
            . "'>"
            . XenForo_Helper_String::wholeWordTrim( $thread['title'],
                40 )
            . "</a></div>"
        );
}
How can that script be configured to only displays threads from specific nodes?
Also, what about showing up to 500 characters from the first post of a thread?
 

LPH

Well-known member
#11
To get a specific node, then add node_id to the array. For example, for node 5

PHP:
'node_id' =>'5'
To show the message then use

PHP:
echo $thread['message'];
You could look at the resource snippet to get an idea how to trim the message.
PHP:
$snippet = $parser->render(XenForo_Helper_String::wholeWordTrim($messageText, 500));
Also, feel free to add a var_dump at any point so you can see what is happening.
 

|Jordan|

Active member
#14
echo $thread['message']; doesn't output the thread message at all for me. In fact, it makes the whole page blank. Also, i just need the first post of the thread to display.

Also, how do i stop the display of smilies, images and media tags?
 
Last edited:
#15
Sorry - I cannot edit the code that was posted. It is wrong though. It contains XenForo_Model_Forum when it should be XenForo_Model_Thread. This code is better.

Thank you @AndyB.

PHP:
<?php

// set the amount of posts to display
$limit = 5;

$startTime = microtime( true );

// Set to forum location - See XenForo index.php for information
$fileDir = '/Applications/MAMP/htdocs/community';

require( $fileDir . '/library/XenForo/Autoloader.php' );
XenForo_Autoloader::getInstance()->setupAutoloader( $fileDir . '/library' );

XenForo_Application::initialize( $fileDir . '/library', $fileDir );
XenForo_Application::set( 'page_start_time', $startTime );

$threadModel = XenForo_Model::create( 'XenForo_Model_Thread' );

$threads = $threadModel->getModelFromCache('XenForo_Model_Thread')->getThreads( array(
    'discussion_state'        => 'visible',
), array(
    'limit'          => $limit,
    'order'          => 'thread_id',
    'orderDirection' => 'desc'
) );

foreach ( $threads AS $thread ) {

        echo(
            "<div class='entry-meta'><a href='"
            . XenForo_Link::buildPublicLink( 'canonical:threads',
                $thread )
            . "'>"
            . XenForo_Helper_String::wholeWordTrim( $thread['title'],
                40 )
            . "</a></div>"
        );
}
Tried your code, but getting a blank page? Any help? Willing to pay for it.
 

LPH

Well-known member
#18
I just got a blank page and no source code?
Make sure you are using the correct path to XenForo. This is line 9 and must point to the XenForo installation.

Also, check your opening php tag. Go ahead and post what you are trying to use.
 

LPH

Well-known member
#20
Did you ever figure this out? I'm trying to do the exact same thing.
I use the above code in many places so it's best for you to paste what you are trying to use.

Also, $thread['message'] can be found by doing the var_dump. For example, in the foreach loop:

PHP:
foreach ( $threads AS $thread ) {
        var_dump($thread);
}
This will show you what is available in $thread. You could also use Zend_Debug::dump($thread); in the loop.