Question

I am using wordpress plugin - 'bbpress' for using a forum. The following code is from the file

/projectname/wp-content/plugins/bbpress/templates/default/bbpress/loop-topics.php

Now the forum is working fine. But I need to add custom code for sorting acording to date, number of replies, and alphabetically in ASC or DESC order. So as you can see I have added the following block of code in there.

 $bbp_loop_args = array(
    'orderby' => 'date',
    'order' => 'DESC',
    );

And this $bbp_loop_args parameter I am sending along with 'while()'. Here is the full code below.

<?php

/**
* Topics Loop
*
* @package bbPress
* @subpackage Theme
*/

?>

<?php do_action( 'bbp_template_before_topics_loop' ); ?>
<?php 
if(bbp_get_forum_topic_count()>0)
    {       

        $bbp_loop_args = array(
        'orderby' => 'date',
        'order' => 'DESC',
        );
        ?>
<ul id="bbp-forum-<?php bbp_forum_id(); ?>" class="bbp-topics">

<li class="bbp-header">

    <ul class="forum-titles">
        <li class="bbp-topic-title"><?php _e( 'Topic', 'bbpress' ); ?></li>
        <li class="bbp-topic-voice-count"><?php _e( 'Voices', 'bbpress' ); ?></li>
        <li class="bbp-topic-reply-count"><?php bbp_show_lead_topic() ? _e( 'Replies', 'bbpress' ) : _e( 'Posts', 'bbpress' ); ?></li>
        <li class="bbp-topic-freshness"><?php _e( 'Freshness', 'bbpress' ); ?></li>
    </ul>

</li>

<li class="bbp-body">

    <?php while ( bbp_topics($bbp_loop_args) ) : bbp_the_topic(); ?>

        <?php bbp_get_template_part( 'loop', 'single-topic' ); ?>

    <?php endwhile; ?>

</li>

<li class="bbp-footer">

    <div class="tr">
        <p>
            <span class="td colspan<?php echo ( bbp_is_user_home() && ( bbp_is_favorites() || bbp_is_subscriptions() ) ) ? '5' : '4'; ?>">&nbsp;</span>
        </p>
    </div><!-- .tr -->

</li>

</ul>
    <?php  } ?>
    <!-- #bbp-forum-<?php bbp_forum_id(); ?> -->

<?php do_action( 'bbp_template_after_topics_loop' ); ?>

But I don't know where I am doing wrong. The forum working fine, but the arguments I am passing seems to be not working and hence the sorting not working. Can anyone reply fast to this issue ? thanks.

Était-ce utile?

La solution

Try this -- Replace your loop code with this,

<li class="bbp-body">
    <?php if ( bbp_has_topics( $bbp_loop_args ) ) : ?>
        <?php while ( bbp_topics() ) : bbp_the_topic(); ?>

            <?php bbp_get_template_part( 'loop', 'single-topic' ); ?>

        <?php endwhile; ?>
    <?php endif;?>
</li>

This may be work...

Default argument array for topics:--

$default = array(
    'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics
    'post_parent' => $default_post_parent, // Forum ID
    'meta_key' => '_bbp_last_active_time', // Make sure topic has some last activity time
    'orderby' => 'meta_value', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
    'order' => 'DESC', // 'ASC', 'DESC'
    'posts_per_page' => bbp_get_topics_per_page(), // Topics per page
    'paged' => bbp_get_paged(), // Page Number
    's' => $default_topic_search, // Topic Search
    'show_stickies' => $default_show_stickies, // Ignore sticky topics?
    'max_num_pages' => false, // Maximum number of pages to show
);

Autres conseils

Can't comment because my rep < 50 so here is it part 2 as an answer :/

To sort by the number of replies:

$bbp_loop_args = array(
    'meta_key' => '_bbp_reply_count',
    'orderby'  => 'meta_value_num'
    );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top