Email
Share

We Rock Your Web Forum » Forums

How to show bbPress topics in Drupal?

(3 posts)
  1. cwd

    senior admin
    Joined: Jul '09
    Posts: 225

    We received this question from a user on our community site. You'll note that we use bbPress to power our forum, and Drupal to power our community site (which you can visit by clicking on the "exit forum" link in the main menu above).

    Here's the code we use to get the last 5 bbPress topics to appear in a sidebar block on our Drupal site:

    <?php
    db_set_active('bbpress');
    unset ($output); // clear previous output
    $list_no =5;
    $sql = "SELECT bb_topics.topic_title, bb_topics.topic_id, bb_topics.topic_slug FROM bb_topics WHERE bb_topics.topic_status = 0 ORDER BY bb_topics.topic_id DESC LIMIT $list_no";
    $output .= "<ul>";
    $result = db_query($sql);
    while ($anode = db_fetch_object($result)) {
    $output .= "<li>".l($anode->topic_title, "http://forum.contractwebdevelopment.com/topic/$anode->topic_slug")."</li>";
    }
    $output .= "</ul>";
    print $output;
    db_set_active('default');
    ?>

    Note that the key is calling the bbPress database in the first line: db_set_active('bbpress');

    To have this work, you'll need to specify two databases in your Drupal's settings.php file, as follows:

    $db_url['default'] = 'mysql://user:pw@localhost/db';
    $db_url['bbpress'] = 'mysql://user:pw@localhost/db';

    Posted 1 year ago #

  2. Posted 1 year ago
  3. Anonymous



    I'm using the new "nicer permalinks" plugin to get rid of /forum/ and /topic/ paths in my URL (because they were showing up as the #1 keywords in Webmaster Tools = not good). Now, the above code redirects, but it would be nice to be able to directly link to my new category based pages. Any ideas how to do this?

    Posted 1 year ago #
  4. cwd

    senior admin
    Joined: Jul '09
    Posts: 225

    Try this:

    <?php
    db_set_active('bbpress');
    unset ($output); // clear previous output
    $sql = "SELECT topic_title, topic_id, topic_slug, forum_slug FROM bb_topics, bb_forums WHERE bb_topics.forum_id = bb_forums.forum_id AND bb_topics.topic_status = 0 ORDER BY bb_topics.topic_last_post_id DESC LIMIT 5";
    $output .= "<ul>";
    $result = db_query($sql);
    while ($anode = db_fetch_object($result)) {
    $output .= "<li>".l($anode->topic_title, "http://forum.example.com/$anode->forum_slug/$anode->topic_slug")."</li>";
    }
    $output .= "</ul>";
    print $output;
    db_set_active('default');
    ?>

    Don't forget to substitute your domain name in for forum.example.com. This snippet shows the last 5 topics based on activity (ie. if someone posted, it moves to the top, as is the default behavior in bbPress). If instead you'd like to simply post the last five topics, replace ORDER BY bb_topics.topic_last_post_id with ORDER BY bb_topics.topic_id. Finally, you can adjust the DESC LIMIT 5 by number to show more or less topics.

    Posted 1 year ago #

RSS feed for this topic

Reply

(required)

Allowed markup: BBcode blockquote code em strong ul ol li font strike center u hr.
You can also put code in between backtick ( ` ) characters.