Personally, I'd rather use the code already provided by MyBB, as it makes it easier and is probably a lot cleaner.
I do it simply like this (for my website, I put all the posts in the announcements forum onto my main page):
On the page you wish to include posts, at the top of the page before everything, put:
<?php
chdir('forums'); // path to MyBB
require './global.php';
require './inc/functions_post.php';
?>
The global gives you all the functions required to retrieve the actual posts. The functions_post gives you the functions to parse the posts and output them on your page.
You can do it easily like this:
$query = $db->query("SELECT * FROM `".TABLE_PREFIX."posts` WHERE `fid` = 2 AND `replyto` = 0 ORDER BY `pid` DESC LIMIT 15");
while ($post = $db->fetch_array($query))
{
$post['date'] = mydate($mybb->settings['dateformat'].", ".$mybb->settings['timeformat'], $post['dateline'], "", 0);
// Parse the message
if($post['smilieoff'] == "yes")
{
$allowsmilies = "no";
}
else
{
$allowsmilies = $forum['allowsmilies'];
}
$post['message'] = postify($post['message'], $forum['allowhtml'], $forum['allowmycode'], $allowsmilies, $forum['allowimgcode'], "yes");
// do me code
if($forum['allowmycode'] != "no")
{
$post['message'] = domecode($post['message'], $post['username']);
}
$avatar_query = $db->query("SELECT `avatar` FROM `".TABLE_PREFIX."users` WHERE `username` = '".$post['username']."' LIMIT 1");
$post_avatar = $db->fetch_array($avatar_query);
$reply_query = $db->query("SELECT * FROM `".TABLE_PREFIX."threads` WHERE `tid` = '". $post['tid'] ."' LIMIT 1");
$thread_replies = $db->fetch_array($reply_query);
You can probably clean that code up a bit. I needed to make several extra variables to target the user table and thread table to get extra info. Basically the user's avatar and how many replies the thread had.
Also take note of the
replyto
= '0', as that makes it so it only retrieves the first post from each thread in the forum. If that's not your aim, then get rid of it.
You may not want all that, so tidy it up to what you want. Also make sure to change the
fid in the first query to the forum id that you wish to pull the posts from.
Finally, just echo the contents to where you want, eg:
if ($post_avatar['avatar'])
{
echo '
<div class="avatar">
<img src="'. $post_avatar['avatar'] .'" alt="User Avatar" />
</div>
';
}
echo '
<div class="subject">'. $post['subject'] .'</div>
<div class="date_author">Posted at '. $post['date'] .' by <a href="forums/member.php?action=profile&uid='. $post['uid'] .'">'. $post['username'] .'</a></div>
<div class="post">'. $post['message'] .'</div>
';
Obviously you'll want to modify that to how you want, as that's just an example. Hope this helps you. It should be pretty straight forward with that.
I also leave it to your responsibility to check for any errors in that code, or any optimisations. I just posted this quickly to give you an idea of how I do it.