MyBB Community Forums

Full Version: [Q] How to post last 5 threads on website homepage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
site:

public_html/index.php

forums:

public_html/forums/index.php

MyBB: 1.8


I want to place the last 5 threads of a specific category on my site index.php

I got this code, but doesn't seem to work.

<?php $query = $db->query("SELECT * FROM ".TABLE_PREFIX."announcements WHERE fid='$x'");
while($announcement = $db->fetch_array($query))
{
eval("\$announcements .= \"".$templates->get("sometemplate")."\";");
} 
							?><p>
								<?php
								$forumquery = $db->query("SELECT fid, name FROM ".TABLE_PREFIX."forums");
while($forum = $db->fetch_array($forumquery))
{
$fid = $forum['fid'];
$threadquery = $db->query("SELECT * FROM ".TABLE_PREFIX."threads WHERE fid=$fid ORDER BY dateline DESC LIMIT 10");
while($thread = $db->fetch_array($threadquery))
{
eval("\$threads .= \"".$templates->get("PKTheme")."\";"); // Put the template name that you want each thread to use.
} // Close thread loop
$postquery = $db->query("SELECT * FROM ".TABLE_PREFIX."posts WHERE fid=$fid ORDER BY dateline DESC LIMIT 10");
while($post = $db->fetch_array($postquery))
{
eval("\$posts .= \"".$templates->get("PKTheme")."\";"); // Put the template name that you want each post to use.
} // Close post loop
} // Close forum loop 
?>

url: www.primeknights.com

Anyone can help me?
You need a template that you are using all those variables on to be evaluated. After that, use the output_page($variablename) function.
Any chance you could explain this a bit more clear? Smile
An example of what dragonexpert means:

mypage.php in the ./ directory

<?php

define("IN_MYBB", 1);
$templatelist = "home_latestthreads,home_latestthreads_thread";
require_once "forums/global.php";
require_once "forums/inc/class_parser.php";
$parser = new postParser;

$threadlist = '';
$query = $db->query("SELECT * FROM ".TABLE_PREFIX."threads t ORDER BY t.lastpost DESC LIMIT 0,5");

while($thread = $db->fetch_array($query)) {
	$lastpostdate = my_date('relative', $thread['lastpost']);
	if($thread['lastposteruid'] == 0) $lastposterlink = $thread['lastposter'];
	else $lastposterlink = build_profile_link($thread['lastposter'], $thread['lastposteruid']);
	
	if(my_strlen($thread['subject']) > 25) $thread['subject'] = my_substr($thread['subject'], 0, 25) . "...";
	$thread['subject'] = htmlspecialchars_uni($parser->parse_badwords($thread['subject']));
	$thread['threadlink'] = get_thread_link($thread['tid']);
	eval("\$threadlist .= \"".$templates->get("home_latestthreads_thread")."\";");
}
if($threadlist) eval("\$latestthreads = \"".$templates->get("home_latestthreads")."\";");

output_page($latestthreads);

Create two new templates: Templates & Style > Templates > Add Template:

First Template:
  • Template Name: home_latestthreads
  • Content:
    <table>
    <tr>
    <td><strong>{$lang->latest_threads}</strong></td>
    </tr>
    {$threadlist}
    </table>

Second Template:
  • Template Name: home_latestthreads_thread
  • Content:
    <tr>
    <td>
    <strong><a href="{$mybb->settings['bburl']}/{$thread['threadlink']}">{$thread['subject']}</a></strong>
    <span class="smalltext"><br />
    by {$lastposterlink}<br />
    {$lastpostdate}
    </span>
    </td>
    </tr>
How would you do this for a specific Forum category only?

So it only displays 5 last posts from the Forum Category : News,

In mybb_forums the fid for that categroy is 2.

i tried something like this but no luck..

                    <?php
      $query = $db->query("
            SELECT *
            FROM mybb_forums 
            WHERE fid= '2'
            ORDER BY lastposter
            LIMIT 0, 5" // Change the last digit to how many recent post you want to be shown
        );

    $list = '';
    while($fetch = $db->fetch_array($query))
    {
        $list .= "<strong><a href=\"community/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
        $poster = "<a href=\"community/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Created by: {$poster}<br />";
        $list .= "<i>" .$fetch['replies']. " Replies</i>";
        $list .= "<i> , " .$fetch['views']. " Views</i><br />";
        $list .= " (<i>Last post by: " .$fetch['lastposter']. "</i>)<br /><hr width=\"50\"><br />";
    
    }
    //output
    echo $list;
?>