Skip to:
Content
Pages
Categories
Search
Top
Bottom

counting forums

  • @clockworker

    Participant

    can anybody a solution to count the subforums and give this counting to the forum.

    – Forum ( Subforum+Subforum2 = 3 topics | 25 posts )

    – Subforum ( 1 topic | 5 posts ) < + 2 ( 2 topics | 20 posts ) = 3 topics | 25 posts

    – Subforum2 ( 2 topics | 20 posts )

Viewing 4 replies - 1 through 4 (of 4 total)
  • @_ck_

    Participant

    update: there is a better version a few posts down

    I had worked this out this quick and dirty routine but then I realized you might mean not to show it within the forum list. If you mean something different let me know.

    In front-page.php

    Change

    <?php  while ( bb_forum() ) :  ?>

    to

    <?php
    forum_totals("init");
    while ( bb_forum() ) :
    forum_totals();
    ?>

    and then this after the end of the forum list routine

    <?php  endwhile;  ?>

    to this

    <?php
    endwhile;
    forum_totals("finish");
    ?>

    This is the main function you can just drop at the bottom of front-page.php

    <?php
    function forum_totals($x="") {
    static $last_parent,$total_posts,$total_topics,$all_topics,$all_posts,$total_counter,$all_counter;
    if (function_exists('bb_get_forum_is_category')) {$category=bb_get_forum_is_category();} else {$category=false;}
    if ($x=="init") {$last_parent=false; $total_topics=0; $total_posts=0; $all_topics=0; $all_posts=0; $total_counter=0; $all_counter=0; return;}
    if ($x=="finish" || $last_parent!==$GLOBALS['forum']->forum_parent || $category) {
    if ($last_parent!==false && $total_counter>1) {echo "<tr><td align='right'>$total_counter subforums:</td><td class='num'>$total_topics</td><td class='num'>$total_posts</td></tr>";}
    $last_parent=$GLOBALS['forum']->forum_parent; $total_topics=$GLOBALS['forum']->topics; $total_posts=$GLOBALS['forum']->posts;
    $total_counter=0;
    } else {$total_topics+= (int) $GLOBALS['forum']->topics; $total_posts+=(int) $GLOBALS['forum']->posts;}
    $all_topics+= (int) $GLOBALS['forum']->topics; $all_posts+=(int) $GLOBALS['forum']->posts;
    if ($x=="finish") {echo "<tr><td align='right'>$all_counter total forums:</td><td class='num'>$all_topics</td><td class='num'>$all_posts</td></tr>";}
    elseif ($category) {$last_parent=false;} else {$all_counter++; $total_counter++; }
    }
    ?>

    @clockworker

    Participant

    my front-page.php http://pastebin.com/d117f4c3a

    i want to countig all topics & posts

    @_ck_

    Participant

    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();

    @jfcarter

    Member

    This worked perfectly for me. Thanks, _ck_!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar