With the continuous development of the Internet, forums have become an important part of online communities, connecting users, sharing information, and facilitating discussions. In Discuz forums, the hot posts feature is highly popular as it quickly recommends trending topics and quality content to users, effectively enhancing user engagement and experience.
The hot posts feature mainly relies on statistical analysis of post data, including views, replies, likes, and other metrics. Using a specific algorithm to comprehensively calculate the popularity score, the system can determine which posts should be marked as hot posts. Depending on different requirements, the evaluation criteria can be flexible, either based solely on views or combining multiple indicators with weighted ranking.
The following example demonstrates how to implement a basic hot posts feature using PHP code. First, add a hot post indicator in the post list template:
<tr>
<td>{$post.subject}</td>
<td>{$post.author}</td>
<td>{$post.views}</td>
<td>{$post.replies}</td>
<td>{if $post.hot == 1}Hot Post{/if}</td>
</tr>
Then, write a backend function to calculate the hotness score of each post, as shown below:
function calculateHotness($post){
return $post['views'] * $post['replies'] / max($post['likes'], 1);
}
foreach($posts as &$post){
$hotness = calculateHotness($post);
if($hotness > 100){
$post['hot'] = 1;
} else {
$post['hot'] = 0;
}
}
This example calculates the hotness by multiplying views with replies and dividing by likes, then determines if the post qualifies as a hot post based on a threshold. The algorithm can be further optimized according to specific project needs to improve hot post filtering.
This article introduced the core principle and PHP implementation example of the hot posts feature in Discuz forums. By designing a reasonable hot post algorithm, forums can effectively increase content exposure and user activity, creating a more engaging interactive environment. Hopefully, this content provides useful reference for implementing and optimizing the hot posts feature.