1. 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?

Discussion in 'XenForo Development Discussions' started by Komori, Jan 15, 2015.

  1. Komori

    Komori Member

    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:
    [​IMG]
    Any suggestions would be greatly appreciated.
     
  2. Brogan

    Brogan XenForo Moderator Staff Member

    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. Komori

    Komori Member

    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.
     
  4. Brogan

    Brogan XenForo Moderator Staff Member

    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.
     
  5. Komori

    Komori Member

    Alright and thanks for the reply. I'll see what I can make happen.
     
  6. LPH

    LPH Well-Known Member

    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>"
            
    );
        }
    }
     
    Geca and HWS like this.
  7. Marcus

    Marcus Well-Known Member

    The most easy way might be just putting a javascript box on your external page that receives the RSS feed from the forum.
     
  8. LPH

    LPH Well-Known Member

  9. LPH

    LPH Well-Known Member

    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 microtimetrue );

    // 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>"
            
    );
    }
     
    AntonyF and creativeforge like this.
  10. |Jordan|

    |Jordan| Active Member

    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?
     
  11. LPH

    LPH Well-Known Member

    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($messageText500));
    Also, feel free to add a var_dump at any point so you can see what is happening.
     
  12. |Jordan|

    |Jordan| Active Member

    Awesome, thanks!

    P.S. I spent the better part of the last hour trying to figure it out myself, but i failed :(
     
  13. LPH

    LPH Well-Known Member

    Glad it is working.
     
  14. |Jordan|

    |Jordan| Active Member

    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: Jan 30, 2015
  15. JeroenR

    JeroenR Member

    Tried your code, but getting a blank page? Any help? Willing to pay for it.
     
  16. LPH

    LPH Well-Known Member

    No need to pay for it. Can you post any error messages being returned?
     
  17. JeroenR

    JeroenR Member

  18. LPH

    LPH Well-Known Member

    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.
     
  19. Tim Jay

    Tim Jay Active Member

    Did you ever figure this out? I'm trying to do the exact same thing.
     
  20. LPH

    LPH Well-Known Member

    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.
     
    Tim Jay likes this.

Share This Page