Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: counting forums


_ck_
Participant

@_ck_

Okay if you want something simple you can make this into a mini-plugin:

(call it forum-totals.php)

<?php
/*
Plugin Name: Forum Totals
*/

function forum_totals() {
global $forum_id;
$forum = get_forum( $forum_id ); $topics=$forum->topics; $posts=$forum->posts;
$forums=get_forums(array('child_of' => $forum_id)); $count=0;
if ($forums) {foreach ($forums as $forum) {$count++; $topics+=$forum->topics; $posts+=$forum->posts;}}
echo "<tr style='background:#ccc;'><td align='right'>$count total forums:</td><td class='num'>$topics</td><td class='num'>$posts</td></tr>";
}

function forum_topics_including_subforums( $forum_id = 0 ) {
echo apply_filters( 'forum_topics', get_forum_topics_including_subforums( $forum_id ), $forum_id );
}

function forum_posts_including_subforums( $forum_id = 0 ) {
echo apply_filters( 'forum_posts', get_forum_posts_including_subforums( $forum_id ), $forum_id );
}

function get_forum_topics_including_subforums( $forum_id = 0 ) {
$forum_id=get_forum_id( $forum_id );
$forum = get_forum( $forum_id ); $topics=$forum->topics;
$forums=get_forums(array('child_of' => $forum_id));
if ($forums) {foreach ($forums as $forum) {$topics+=$forum->topics;}}
return apply_filters( 'get_forum_topics', $topics, $forum_id );
}

function get_forum_posts_including_subforums( $forum_id = 0 ) {
$forum_id=get_forum_id( $forum_id );
$forum = get_forum( $forum_id ); $posts=$forum->posts;
$forums=get_forums(array('child_of' => $forum_id));
if ($forums) {foreach ($forums as $forum) {$posts+=$forum->posts;}}
return apply_filters( 'get_forum_posts', $posts, $forum_id );
}
?>

Install and activate it and then you need to put

<?php forum_totals(); ?>

anywhere you want to see it, on front-page.php or forum.php templates.

It should go between the endwhile and the </table>

Like this:

<?php  endwhile; ?>
<?php forum_totals(); ?>
</table>

To use the replacement functions for forum_topics() and forum_posts() which include the totals for a forum including it’s subforums, simply replace them in your templates with forum_topics_including_subforums(); and forum_posts_including_subforums();

Skip to toolbar