Skip to:
Content
Pages
Categories
Search
Top
Bottom

Excerpts

  • 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.

Viewing 19 replies - 1 through 19 (of 19 total)

  • chrishajer
    Participant

    @chrishajer

    Why not just use a blog with multiple author’s?

    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?


    livibetter
    Member

    @livibetter

    I think you can just copy/use WordPress’ excerpt generating function, just a thought but I know that will work.

    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.


    livibetter
    Member

    @livibetter

    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.

    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.

    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.


    livibetter
    Member

    @livibetter

    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.

    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.


    livibetter
    Member

    @livibetter

    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.

    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.

    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.


    livibetter
    Member

    @livibetter

    @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.

    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.

    I like that

    how can we get the forst post instead of the last one ?

    Anybody ?

    That would also be great to have excerpt for the rss feed

    There is no point for a feed if it doesn’t brings you any traffic.


    inboedelverzekering
    Member

    @inboedelverzekering

    Salemw, I think you should settle with this!

    Well my friend found a solution for me and in case I wasn’t the only one asking for excerpt RSS

    here it is:

    Add this function to functions.bb-template:

    // excerpt for rss
    function post_text_excerpt( $post_id = 0 ) {
    echo apply_filters( 'post_text', get_post_text_excerpt( $post_id ), get_post_id( $post_id ) );
    }

    function get_post_text_excerpt( $post_id = 0 ) {
    $bb_post = bb_get_post( get_post_id( $post_id ) );
    // Number max of characters
    $max_rss=250;
    $excerpt_rss = $bb_post->post_text;

    if (strlen($excerpt_rss)>$max_rss) {
    $excerpt_rss = substr(ltrim($excerpt_rss), 0, $max_rss);
    preg_match('<code>.+(?=[,;.])</code>s', $excerpt_rss, $out1);
    preg_match('<code>.+(?=[ ])</code>s', $excerpt_rss, $out2);
    if ( ( strlen($out2[0]) - strlen($out1[0]) ) < $tronque_maxi) {
    $excerpt_rss = $out1[0];
    } else {
    $excerpt_rss = $out2[0];
    }
    if (preg_match('<code>.+(?=(de|du|dans|le|la|a|à)$)</code>s', $excerpt_rss, $out3)) {$excerpt_rss = $out3[0];};
    return $excerpt_rss."...";
    } else {
    return $excerpt_rss;
    }
    return apply_filters( 'get_post_text', $excerpt_rss, $bb_post->post_id );
    }
    // end excerpt for rss

    And replace post_text() par post_text_excerpt() in my-templates/your_template/rss2.php

    The author website:

    http://messouvenirs.net/


    johnnydoe
    Member

    @johnnydoe

    has anyone already figured out how to show the excerpt with livibetters code of the topic (main post) instead of the last comment?

    thx

Viewing 19 replies - 1 through 19 (of 19 total)

You must be logged in to reply to this topic.