1. 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.
  2. In order to post messages and view attached files in this forum, you must be a licensed XenForo customer.
    If you can't post, enter your forum username in the Associated Forum Users form in your customer area.

Latest posts on your website

Discussion in 'Development Tutorials [Archive]' started by Liam Dawe, Dec 4, 2010.

  1. Liam Dawe Active Member

    Here is a quick and simple way to get a list of the latest posts on your website :)

    NOTE: This is not meant to be inside xenforo nor has it been designed in anyway to be used like that, it's for a quick last post block/latest news section on your actual website.

    You will need this brilliant helper class by Shadab https://gist.github.com/722240
    Simply download it and upload the file to "/library/GeekPoint/Symfony.php" (you will need to make the directory GeekPoint).

    DEMO: http://www.gamingonlinux.com/posts.php

    Put this at the very top of your page:
    PHP:

    <?php
    // get latest x forum posts from xenforo by liamdawe

    // edit this to whatever folder your forum is in
    $forum_url 'chill/';

    // change to the amount of posts you want displayed
    $limit 7;

    // edit this to the directory your forum is in
    $forum_directory '/home/website/forum/';

    $startTime microtime(true);

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

    GeekPoint_Symfony::initializeXenforo($forum_directory$startTime);

    $nodeModel XenForo_Model::create('XenForo_Model_Node');
    $nodes $nodeModel->getViewableNodeList();

    $nodes_get array_keys($nodes);

    $node_id_list implode($nodes_get',');

    $sql_forum "SELECT `title`, `thread_id`, `view_count`, `reply_count` FROM `xf_thread` WHERE `node_id` IN ($node_id_list) ORDER BY `last_post_date` DESC LIMIT {$limit}";

    $topics_query XenForo_Application::get('db')->fetchAll($sql_forum);
    ?>
    Put this where you want it to display.
    PHP:

    foreach ($topics_query as $thread)
    {
        
    // The absolute thread url is constructed for you
        
    $threadUrl XenForo_Link::buildPublicLink('full:threads'$thread);

        
    // Trimmed and properly escaped
        
    $threadTitle XenForo_Template_Helper_Core::helperWordTrim($thread['title'], 50);

        echo 
    "<a href=\"$threadUrl\">$threadTitle</a> Views: {$thread['view_count']}, Comments: {$thread['reply_count']}<br />
    <br />"
    ;
    }
    Recent Changes:
    05/12/2010 - Added ability to limit it to certain forums or just all of them.
    05/12/2010 - Added double quote " filtering. - Edit done twice as it should replace with nothing not a -.
    05/12/2010 - Added in config file includes so you don't need to edit any database stuff now.
    06/12/2010 - Takes users permissions for viewing nodes into account as requested.
    04/03/2011 - It now uses xenforo's database system to generate the query, should fix random errors.
    20/08/2011 - Now uses xenforo's functions to construct the url (thanks shadab) and you can put in views and comment counts (replys).
    bousaid, Azaly, ToDie4 and 8 others like this.
  2. VincentU Well-Known Member

    Don't you need any user and pass to connect to the db?
  3. Liam Dawe Active Member

    Edited it, my bad jumping the gun thinking people would already be doing that :p
  4. ragtek back2life

    Next add-on which doesn't check user permissions:D
    That's so funny, nobody here have private forum categories (for example mod,admin categorie) which normal users aren't allowed to read?
  5. Liam Dawe Active Member

    I don't but i will edit the code to include the option of selecting all forums or only certain forums for people who do.
  6. VincentU Well-Known Member

  7. Liam Dawe Active Member

  8. VincentU Well-Known Member

    It is :)
    Nice :D

    If anyone needs an example on how to put it in a valid file with a list, here it is ^^
    HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
        <title>Latest X Threads</title>
        <meta http-equiv="Content-Language" content="en-us" />
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    
    <body>
        <ul>
            <?php
            // get latest x forum posts from xenforo by liamdawe
            // change these to your database information
            $database_host = 'localhost'; // usually left as it is!
            $database_username = 'usr';
            $database_password = 'pwd';
            $database_db = 'db';
    
            $forum_url = ''; // edit this to whatever folder your forum is in
    
            $limit = 5; // change to the amount of posts you want displayed
    
            $forum_ids = 'ALL'; // enter either "ALL" or forum id's seperated by commas so "1,2,3"
    
            mysql_connect($database_host, $database_username, $database_password);
            mysql_select_db($database_db);
    
            $where = '';
            if ($forum_ids != 'ALL')
            {
                $where = 'WHERE `node_id` IN ($forum_ids)';
            }
    
            $sql_forum = "SELECT `title`, `thread_id` FROM `xf_thread` {$where} ORDER BY `last_post_date` DESC LIMIT {$limit}";
    
            $query_forum = mysql_query($sql_forum);
                while ($topics = mysql_fetch_assoc($query_forum))
                {
                    $url_title = str_replace(' ', '-', $topics['title']);
                    $url_title = str_replace('.', '', $url_title);
                    $url_title = str_replace('?', '', $url_title);
                    $url_title = str_replace('/', '-', $url_title);
                    $url_title = str_replace('\\', '-', $url_title);
                    $url_title = str_replace('"', '-', $url_title);
                    echo "<li><a href=\"{$forum_url}index.php?threads/{$url_title}.{$topics['thread_id']}/\">{$topics['title']}</a></li>";
                }
            ?>
        </ul>
    </body>
    
    </html> 
    Liam Dawe likes this.
  9. Liam Dawe Active Member

    Good job, may want to slightly adjust the double quote filter to be '' rather than '-'.
  10. VincentU Well-Known Member

    Does it really matter? :)
    It's working fine
  11. Liam Dawe Active Member

    It doesn't really matter it's just for how it looks really, works fine either way but it shouldn't replace it, it should remove it - just lucky it works anyway.

    Hope some people find this useful.
  12. VincentU Well-Known Member

    I'm sure many will :)

    It's easy for when you have a custom theme as homepage :D
  13. Liam Dawe Active Member

    If anyone has any suggestions please do keep them coming :)
  14. VincentU Well-Known Member

    I would suggest that you also create a version where it uses the values of config.php of XenForo.
  15. Liam Dawe Active Member

    Good idea i will implement that in a bit, will make things a tiny bit quicker for people :)
  16. patrick91 Member

    Is it possible to make it in xen syntax? because i can't put it in a template right? i get message about xen syntax...

    Don't know if it is built in yet but maybe handy to have a limit of cheracters per post to display?

    Patrick
  17. Liam Dawe Active Member

    Its meant for outside of xenforo as a latest post display, its not a template mod.
  18. patrick91 Member

    Yeah i see! needed on for template... :D but nice job!
  19. Floris Guest

    Instead of specific forums, I rather have the permissions properly checked..
    As admin I want to get new admin content.
  20. Liam Dawe Active Member

    Added the config info from your install so you don't need to edit database stuff now.

    As for permissions -> once it is more documented and their are a few guides to their code i can look into using it, one step at a time ;)

Share This Page