3
votes

I want to display posts of a user in a custom tab on members' buddypress profile.

Problem: The pagination does not work.

What I'm trying: Here's the code that I'm trying:

Create tab: The function below creates a tab on the members profile page. It works fine.

function add_profile_tab() {
    global $bp;
    bp_core_new_nav_item( array( 
        'name'                =>   'Blog',
        'slug'                =>   'blog', 
        'screen_function'     =>   'yourtab_screen', 
        'position'            =>   80,
        'parent_url'          =>   bp_loggedin_user_domain() . '/blog/',
        'parent_slug'         =>   $bp->profile->slug,
        'default_subnav_slug' =>   'blog'
    ) );
}

add_action( 'bp_setup_nav', 'add_profile_tab' );

function yourtab_screen() {
    add_action( 'bp_template_title', 'blog_content_title' );
    add_action( 'bp_template_content', 'blog_content_main' );
    bp_core_load_template( 'buddypress/members/single/plugins' );
}

function blog_content_title() {
    echo 'Title';
}

function blog_content_main() { 
    show_user_posts();
}

Displaying User Posts: Here's the show_user_posts() that displays the user posts:

function show_user_posts() {
    $args = array(
        'author' => bp_displayed_user_id(),
        'post_status' => 'publish',
        'paged'  => $paged
    );  

    $query = new WP_Query( $args ); 

    if ( $query -> have_posts() ) :

        while ( $query -> have_posts() ) : $query -> the_post();    
            get_template_part( 'excerpt', 'content' );                                      
        endwhile;                   

        blog_pagination( $query->max_num_pages, '', $paged );
        wp_reset_postdata();
    endif; 
}

Pagination: Here's the pagination function.

function blog_pagination( $numpages = '', $pagerange = '', $paged = '' ) {

    global $paged, $wp_query, $wp_rewrite;  

    if ( empty( $pagerange ) ) {
        $pagerange = 2;
    }

    if ( empty( $paged ) ) {
        $paged = 1;
    }

    if ( $numpages == '' ) {
        $numpages = $wp_query -> max_num_pages;

        if ( ! $numpages ) {
            $numpages = 1;
        }
    }

    $pagination_args = array (
        'base'            =>   get_pagenum_link(1) . '%_%',
        'format'          =>   'page/%#%',
        'total'           =>   $numpages,
        'current'         =>   $paged,
        'show_all'        =>   False,
        'end_size'        =>   1,
        'mid_size'        =>   $pagerange,
        'prev_next'       =>   True,      
        'type'            =>   'plain',
        'add_args'        =>   false,
        'add_fragment'    =>   ''
    );

    if ( $wp_rewrite->using_permalinks() )
        $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ).'page/%#%/', 'paged' );

    if ( ! empty( $wp_query->query_vars['s'] ) )
        $pagination_args['add_args'] = array( 's' => get_query_var( 's' ) );

    $paginate_links = paginate_links($pagination_args);

    if ($paginate_links) {
        echo $paginate_links;
    }
}

What happens: The above pagination function calculates and generates the number of pages correctly, however I get 404 error on all pagination links. For example on this url: http://wp/members/admin/blog/page/2/

What I think: This is being caused by rewrite rules. It can be fixed by adding a rewrite rule for the above URL. In the WordPress that can be done using add_rewrite_rule but I can't figure out the url structure for the Buddypress.

2
Hi there. Could you please give a correct Url of your website? The one you gave is wrong: link, your website domain is missingbkseen
I'm also having this problem, did you figure out the resolution?Mark

2 Answers

0
votes

Use DataTable it will be gives you automatic pagination function just you have to control that function on basis of your needs. Here i am giving you link see this https://datatables.net/examples/ajax/simple.html or For Wordpress you can also use Plugin as named WP-Paginate.

0
votes

Reset posts per page of particular page,post, taxonomy etc.

Note : This will conflict with WordPress' default settings of 10 posts per page. If you are testing pagination go to Settings > Reading in the admin. & you can change post per page by using below function for particular post. just change in if condition.

add below function in function.php file

$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
    add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
    global $option_posts_per_page;
    if ( !is_admin() && bp_has_members()  ) {
        return 12;
    } else {
        return $option_posts_per_page;
    }
}