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

Member 3639

Active member
#1
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).
 
R

ragtek

Guest
#4
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?
 

Vincent

Well-known member
#8
Should be fixed with the new code, i didn't filter out double quotes :)
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>
 
#11
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.
 
#16
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
 
F

Floris

Guest
#19
Instead of specific forums, I rather have the permissions properly checked..
As admin I want to get new admin content.
 
#20
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 ;)