1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. 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.

Latest posts on your website

Discussion in 'Development Tutorials [Archive]' started by Member 3639, Dec 4, 2010.

  1. Member 3639

    Member 3639 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:
    // 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);

    XenForo_Autoloader::getInstance()->setupAutoloader($forum_directory '/library');


    $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.
    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);

    "<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).
    vfustar, Eagle, bousaid and 10 others like this.
  2. Vincent

    Vincent Well-Known Member

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

    Member 3639 Active Member

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

    ragtek Guest

    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. Member 3639

    Member 3639 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. Vincent

    Vincent Well-Known Member

  7. Member 3639

    Member 3639 Active Member

  8. Vincent

    Vincent 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 ^^
    <!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">
        <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" />
            // 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);
            $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>";
    Member 3639 likes this.
  9. Member 3639

    Member 3639 Active Member

    Good job, may want to slightly adjust the double quote filter to be '' rather than '-'.
  10. Vincent

    Vincent Well-Known Member

    Does it really matter? :)
    It's working fine
  11. Member 3639

    Member 3639 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. Vincent

    Vincent Well-Known Member

    I'm sure many will :)

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

    Member 3639 Active Member

    If anyone has any suggestions please do keep them coming :)
  14. Vincent

    Vincent Well-Known Member

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

    Member 3639 Active Member

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

    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?

  17. Member 3639

    Member 3639 Active Member

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

    patrick91 Member

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

    Floris Guest

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

    Member 3639 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