Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to: Pagination on a customized friends list


  • webfacemedia
    Participant

    @webfacemedia

    I have a custom buddypress friends list page that displays the friends videos and I’ve set my per_page to 50. How can I paginate the friends list in the event a user has more than 50 friends?

    <div class="dashboard-wrapper">
    <h1>My Friends' Videos</h1>
    <a href="?part=default" class="button">Dashboard</a><br>
    <?php
    $user_id = bp_is_user() ? bp_displayed_user_id() : bp_loggedin_user_id();
    
    $args = apply_filters('bp_show_friends_args', array(
        'user_id' => $user_id,
        'type' => 'active',
        'per_page' => 50,
        'max' => $limit,
        'populate_extra' => 0
            )
    );
    
    if (bp_has_members($args)) {
        ?>
        <ul class="bpsf-members">
            <?php while (bp_members()) : bp_the_member(); ?>
    
                <li class="bpsf-member">
                    <?php bp_member_name(); ?>
                    <div class="item-avatar">
                        <a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name(); ?>">
                            <?php bp_member_avatar($avatar_args) ?><?php display_friend_colors(bp_get_member_user_id()); ?>
                        </a>
    
                    </div>
                    <?php do_action('bp_show_friends_after_friend_avatar', bp_get_member_user_id()); ?><div class="cleared"></div>
                    <div class="videos">
                        <ul>
                            <?php
                            $args = array(
                                'author' => bp_get_member_user_id(),
                                'orderby' => 'post_date',
                                'order' => 'DESC',
                                'post_type' => 'member_video',
                                'posts_per_page' => -1 // no limit
                            );
                            $current_user_posts = get_posts($args);
                            foreach ($current_user_posts as $post) : setup_postdata($post);
                                $thumbnail = get_post_meta(get_the_ID(), 'thumbnail');
                                //d($thumbnail);
                                ?>
                                <li>
                                    <a href="<?php the_permalink(); ?>"><img src="<?= $thumbnail[0] ?>" /></a>
                                    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                                </li>
                                <?php
                            endforeach;
                            wp_reset_postdata();
                            ?>
                        </ul>
                    </div>
                    <div class="cleared"></div>
                </li>
    
            <?php endwhile; ?>
        </ul>
    <?php }
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)

  • Varun Dubey
    Participant

    @vapvarun

    Seeems you haven’t set the paged variable correctly.
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    and you will also need to add the following inside $args

    'paged' => $paged,
    posts_per_page=50

    webfacemedia
    Participant

    @webfacemedia

    Thanks for this. And where/how do I add the pagination links?


    webfacemedia
    Participant

    @webfacemedia

    @vapvarun : I’ve modified the code but none of the standard pagination links seem to work

        $user_id = bp_is_user() ? bp_displayed_user_id() : bp_loggedin_user_id();
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = apply_filters('bp_show_friends_args', array(
            'user_id' => $user_id,
            'type' => 'active',
            'per_page' => 2,
            'paged' => $paged,
           // 'max' => $limit,
            'populate_extra' => 0
                )
        );

    webfacemedia
    Participant

    @webfacemedia

    SOLVED:

    <div class="dashboard-wrapper">
        <h1>My Friends' Videos</h1>
        <a href="?part=default" class="button">Dashboard</a><br><br><hr>
        <?php
        $user_id = bp_is_user() ? bp_displayed_user_id() : bp_loggedin_user_id();
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = apply_filters('bp_show_friends_args', array(
            'user_id' => $user_id,
            'type' => 'active',
            'per_page' => 20,
            'paged' => $paged,
            // 'max' => $limit,
            'populate_extra' => 0
                )
        );
    
        if (bp_has_members($args)) {
            ?>
            <div id="pagination">
                <ul class="pagination">
                    <li id="previous-stories" class="arrow">
                        <?php bp_members_pagination_links(); ?>
                    </li>
                 </ul>
            </div><!-- /#pagination --><hr>
            <ul class="bpsf-members">
                <?php while (bp_members()) : bp_the_member(); ?>
    
                    <li class="bpsf-member">
                        <?php bp_member_name(); ?>
                        <div class="item-avatar">
                            <a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name(); ?>">
                                <?php bp_member_avatar($avatar_args) ?><?php display_friend_colors(bp_get_member_user_id()); ?>
                            </a>
    
                        </div>
                        <?php do_action('bp_show_friends_after_friend_avatar', bp_get_member_user_id()); ?><div class="cleared"></div>
                        <div class="videos">
                            <ul>
                                <?php
                                $args = array(
                                    'author' => bp_get_member_user_id(),
                                    'orderby' => 'post_date',
                                    'order' => 'DESC',
                                    'post_type' => 'member_video',
                                    'posts_per_page' => -1 // no limit
                                );
                                $current_user_posts = get_posts($args);
                                foreach ($current_user_posts as $post) : setup_postdata($post);
                                    $thumbnail = get_post_meta(get_the_ID(), 'thumbnail');
                                    //d($thumbnail);
                                    ?>
                                    <li>
                                        <a href="<?php the_permalink(); ?>"><img src="<?= $thumbnail[0] ?>" /></a>
                                        <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                                    </li>
                                    <?php
                                endforeach;
                                wp_reset_postdata();
                                ?>
                            </ul>
                        </div>
                        <div class="cleared"></div>
                    </li>
    
                <?php endwhile; ?>
            </ul>
        <?php }
        ?>
    
    </div>
    

    Varun Dubey
    Participant

    @vapvarun

    Cool 🙂

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