bbPress

Simple, Fast, Elegant

bbPress support forums » Plugins

Excerpts

(15 posts)
  • Started 11 months ago by Doobus
  • Latest reply from Vedmak
  • This topic is not resolved

Tags:

  1. Doobus
    Member

    I'm in the process of theming bbPress not to look like a forum, but rather a blog with multiple posters :P. The one thing I'm stuck on, and there doesn't seem to be any documentation on how to do this. How do you put excerpts in the front? The best example would have to be 9rules, I was wondering if that was custom code, or is there a plugin or a simple solution to achieve that? Thanks in advance.

    Posted 11 months ago #
  2. Why not just use a blog with multiple author's?

    Posted 11 months ago #
  3. Doobus
    Member

    I've already looked into that as a viable solution, but Wordpress' back-end just complicates things for new users. bbPress is a lot simpler solution in that a forum is built around the idea of community, I'm just theming it not to look like a forum for advertising purposes, everyone knows that the ctr for forums are terrible. Plus I already gutted Kakumei beyond recognition, can't turn back now :).

    Anyhoo, anyone know of how to put excerpts?

    Posted 11 months ago #
  4. I think you can just copy/use WordPress' excerpt generating function, just a thought but I know that will work.

    Posted 11 months ago #
  5. Doobus
    Member

    I'm not sure if you meant "I don't know that will work." or "I know that will work." Unfortunately for me it didn't work. Thanks for suggesting it tho.

    Posted 11 months ago #
  6. If you just call the template functions of WordPress, that won't work. I meant get the part you need. (sorry for misleading)

    PS. If you still can't get it work, please paste your code and error message, if any.

    Posted 11 months ago #
  7. Doobus
    Member

    I'm mediocre at best when it comes to php, so I'm a bit confused as to what to call, all I really know are the functions for Wordpress, any code suggestions would be greatly appreciated.

    Posted 11 months ago #
  8. You may want to think quite carefully about doing this, as it's not the simplest of tasks. The key issue is that bbpress does not have the data you want at the time you want it.

    Basically bb only looks into the bb_topics table to see what the latest topics are. That tells it what the last post id was, what the last poster's name was and all that, but it doesn't tell it what the content of the last post was. To do that, you need to grab the ids of every last post being displayed, then write a database query to get the data from bb_posts. That can (*can*) be done in a single query though I think, much as bb grabs all the topics at once, so it's not too bad. Then you have the data and can play with it however you want.

    So it takes some neat hooking and querying.

    Posted 11 months ago #
  9. Replace the similar part with it in front-page.php (Kakumei):

    <?php if ( $topics ) : foreach ( $topics as $topic ) : ?>
    <tr<?php topic_class(); ?>>
        <td><?php bb_topic_labels(); ?> <a href="<?php topic_link(); ?>"><?php topic_title(); ?></a>
    <?php
    // from wp_trim_excerpt() in WordPress 2.3.1 formatting.php, just removed few lines
    function make_excerpt($text) { // Fakes an excerpt if needed
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = 55;
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words) > $excerpt_length) {
            array_pop($words);
            array_push($words, '[...]');
            $text = implode(' ', $words);
        }
        return $text;
    }
    
    echo make_excerpt( get_post_text( $topic->topic_last_post_id ) );
    ?>
        </td>
        <td class="num"><?php topic_posts(); ?></td>
        <td class="num"><?php topic_last_poster(); ?></td>
        <td class="num"><small><?php topic_time(); ?></small></td>
    </tr>
    <?php endforeach; endif; // $topics ?>

    It generates an excerpt of latest reply of topic.

    Posted 11 months ago #
  10. Look out for the fact that this will, as far as I know, give you 30 extra database queries per page, that it normally takes about 10 to show a page, and that db queries tend to be the main bottleneck on servers. This will work, but to get rid of those 29 unnecessary queries you need more subtlety.

    Posted 11 months ago #
  11. fel64 is right. So I made a change.

    <?php
    // from wp_trim_excerpt() in WordPress 2.3.1 formatting.php, just removed few lines
    function make_excerpt($text) { // Fakes an excerpt if needed
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = 55;
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words) > $excerpt_length) {
            array_pop($words);
            array_push($words, '[...]');
            $text = implode(' ', $words);
        }
        return $text;
    }
    
    if ( $topics ) :
        $last_post_ids = array();
        foreach ( $topics as $topic )
            $last_post_ids[] = $topic->topic_last_post_id;
        global $bbdb;
        $post_ids = $bbdb->get_col( "SELECT post_id, post_text FROM $bbdb->posts WHERE post_id IN (" . implode(',', $last_post_ids) . ")");
        $post_texts = $bbdb->get_col( null, 1 );
        $post_excerpts = array();
        foreach($post_ids as $idx => $post_id)
            $post_excerpts[$post_id] = make_excerpt( $post_texts[$idx] );
    endif;
    ?>
    <?php if ( $topics ) : foreach ( $topics as $topic ) : ?>
    <tr<?php topic_class(); ?>>
        <td><?php bb_topic_labels(); ?> <a href="<?php topic_link(); ?>"><?php topic_title(); ?></a>
    <?php echo $post_excerpts[$topic->topic_last_post_id]; ?>
        </td>
        <td class="num"><?php topic_posts(); ?></td>
        <td class="num"><?php topic_last_poster(); ?></td>
        <td class="num"><small><?php topic_time(); ?></small></td>
    </tr>
    <?php endforeach; endif; // $topics ?>

    On my test forum, 15 posts, without excerpts takes .119 sec, with excerpts takes .127 sec. Previous code takes .140 sec.

    Posted 11 months ago #
  12. Doobus
    Member

    Amazing work, however I haven't tested this on a live server yet, so probable use based on speed is still up in the air. Thanks a lot for putting this together Livibetter.

    Posted 11 months ago #
  13. livibetter, if you can separate the logic code from the presentation it'd be even better. You can always put a hook into the template and put the code into a plugin, or use the query filter to find when the topics are being looked for.

    Posted 11 months ago #
  14. @fel64: I agree. If I use this on my forums, I will put it in my-forums-only plugin. However, this is a simple help, I don't want to show them a complicated solution. Yes, easiness can be hardness later, that's a trade-off.

    Moreover, even I do what you suggestion, that is still not an art in my mind. It manipulates something, it's not beautiful.

    edit: One off-topic thing, where is the location of your website? I hardly to connect to it. Sometimes, I got Server not found.

    Posted 11 months ago #
  15. Vedmak
    Member

    This is great excerpt , but i have 2 problems:

    1. excerpt don't work with Sticky Topic
    2. how to make plugin from this code?

    Thanks anyway livibetter.

    Posted 10 months ago #

RSS feed for this topic

Reply

You must log in to post.

Code is Poetry.