Show posts from your SMF forum in your home or index page
SMF forum is quit popular open source forum application used by many websites as their main bulletin board. Some time we need to grab posts, topics from the board and show them on our home/index page or anywhere on our main website.
To make the things much easier, SMF provides all this in one script where we call a function with arguments and dress it up according to our needs. File is SSI.php and can be found in the root folder of the forum. You can also add extra parameters/piece of code to your SSI.php. In this post i will show you how to retrieve posts from SMF board with additional information like board, post author, date & time plus top poster and board statistics.
$PostList = ssi_recentPosts(8, null, null, 'array');
The above piece of code will pull the last 8 posts from the forum db, as you can see i used array this is because i want to dress up my code like open links in new tab/window etc.
foreach ($PostList as $Post) {
//echo '<pre>', print_r($Post), '</pre>';
echo "<tr>";
echo "<div class='forum_posts'>";
echo '<td width="350px"><a target="_blank" href=', $Post['href'], '>', $Post['subject'], '</a></td>',
'<td width="250px"><a target="_blank" href=', $Post['board']['href'], '>',$Post['board']['name'],'</a></td>',
'<td width="100px"><a target="_blank" href=', $Post['poster']['href'], '>', $Post['poster']['name'], '</a></td>',
'<td width="150px">',$Post['time'],'</td>';
echo "</div>";
echo "</tr>";
}
unset($PostList);
The above code will loop the 8 results and print it on the page with post subject, board, poster name and date.
Now the additional stuff like top poster and board statistics:
<strong>Top Poster : </strong><?php ssi_topPoster();?> <hr id='line-hr'>
<strong>Forum Stats : </strong><?php ssi_boardStats(); ?> <hr id='line-hr'>
This piece of code will pull the things for you, now lets put the whole code in one place with html and its css.
Full code:
<h2>My Forum Recent Happenings!</h2>
<strong>Recent Posts : </strong>
<table class='main'>
<tr>
<th>Post</th><th>Board</th><th>Posted by</th><th>Time</th>
</tr>
<?php
$PostList = ssi_recentPosts(8, null, null, 'array');
foreach ($PostList as $Post) {
echo "<tr>";
echo "<div class='forum_posts'>";
echo '<td width="350px"><a target="_blank" href=', $Post['href'], '>', $Post['subject'], '</a></td>',
'<td width="250px"><a target="_blank" href=', $Post['board']['href'], '>',$Post['board']['name'],'</a></td>',
'<td width="100px"><a target="_blank" href=', $Post['poster']['href'], '>', $Post['poster']['name'], '</a></td>',
'<td width="150px">',$Post['time'],'</td>';
echo "</div>";
echo "</tr>";
}
unset($PostList);
?>
</table>
<strong>Top Poster : </strong><?php ssi_topPoster();?> <hr id='line-hr'>
<strong>Forum Stats : </strong><?php ssi_boardStats(); ?> <hr id='line-hr'>
You can put this code any where you want in you pages, just to make sure include the css below in the head tag of the header file(home/index etc etc) like:
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
CSS file code(style.css):
/* Table Layout */
table.main {
margin-top: .5em;
margin-bottom: .5em;
margin-left: .0em;
margin-right: .0em;
background: whitesmoke;
border-collapse: collapse;
font-size: 13px;
font-family: Arial;
}
table.main tr:hover {
background: #E6E6E6 !important;
}
table.main td {
border: 0px silver solid;
padding: 0.5em;
text-align:left;
border-bottom:1px silver solid;
}
table.main th {
border: 0px silver solid;
border-bottom:2px silver solid;
padding: 0.5em;
background: gainsboro;
text-align: left;
}
table.main caption {
margin-left: inherit;
margin-right: inherit;
}
/* Table Laout Ends */
/* Hr */
#line-hr {
height:1px;
margin-top:7px;
margin-bottom:7px;
background-color: silver;
border: 0px solid;
}
/* Hr Ends */
/* Forum Posts */
.forum_posts {
margin-top: 5px;
margin-bottom: 5px;
border-bottom: 0px solid #000;
}
.forum_posts a:link {
text-decoration: underline;
}
.forum_posts a:hover {
text-decoration: none;
color: #ea4900;
}
.forum_posts #time {
float: right;
}
/* Forum Posts Ends */
Save this file as style.css and include it in your head tag of the home/index page.