dongsimang4036 2017-07-24 07:28 采纳率: 100%
浏览 57

BuddyPress会员资料中的分页

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条回答 默认 最新

  • dousu5608 2017-07-27 12:02
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大