admin管理员组

文章数量:1122846

I created a new tab called "Videos" inside of the BuddyPress Profile page. This tab contains all video posts added by that user. The problem is that it only shows the first 12 posts and the pagination does not show up. I did try to include the pagination code provided by the theme but to no avail.

Notes:

  • I am using a theme called "VideoTube"

  • The post loop to be paginated is from a custom post type called "video"

  • The original pagination code provided by the theme is <?php do_action( 'mars_pagination', null );?>

  • The code below is pulled from my functions.php file in the child theme folder.

Thank you in advance for all your help :D

Code:

    <?php function my_videos_screen_content() { 
?>
<div class="container">
        <div class="row">
        <div class="col-sm-8">
            <div class="row video-section meta-maxwidth-230">
<?php 

$args = array(
    'post_type'         => array( 'video' ),
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$author_videos = new WP_Query( $args );

if ( $author_videos->have_posts() ) : while ( $author_videos->have_posts() ) : $author_videos->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <?php do_action( 'mars_pagination', null );?>
    <?php wp_reset_postdata();
    endif; ?>
        </div>
    </div>
</div>
</div>  
    <?php }

So I figured out how to show the pagination, with the following code. The problem is when I click on the next page, it brings me to a 404 page. But when I manually visit a custom URL it does show the second page correctly, here are both examples:

404 Version:

This is the url structure that is generated by this pagination code

www.website/members/username/my-videos/page/2/

Working Version:

This is a manual url I found to be working

www.website/members/username/my-videos/?page=2

Question is, how to get it to work with the first version (/page/2/)

CODE:

    <?php function my_videos_screen_content() { 
echo 'We are currently working on this section, some content may not appear properly. Thank you for your patience.'; ?>
<div class="container">
        <div class="row">
        <div class="col-sm-8">
            <div class="row video-section meta-maxwidth-230">
<?php 

$custom_query_args = array(
    'post_type'         => 'video',
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$custom_query = new WP_Query( $custom_query_args );

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <?php endif; 
    wp_reset_postdata(); ?>

    <?php // Custom query loop pagination
    previous_posts_link( 'Newer Videos' );
    next_posts_link( 'Older Videos', $custom_query->max_num_pages );?>

    <?php // Reset main query object
    $wp_query = NULL;
    $wp_query = $temp_query; ?>

        </div>
    </div><!-- /.row -->
</div>
</div><!-- /.container -->   

I created a new tab called "Videos" inside of the BuddyPress Profile page. This tab contains all video posts added by that user. The problem is that it only shows the first 12 posts and the pagination does not show up. I did try to include the pagination code provided by the theme but to no avail.

Notes:

  • I am using a theme called "VideoTube"

  • The post loop to be paginated is from a custom post type called "video"

  • The original pagination code provided by the theme is <?php do_action( 'mars_pagination', null );?>

  • The code below is pulled from my functions.php file in the child theme folder.

Thank you in advance for all your help :D

Code:

    <?php function my_videos_screen_content() { 
?>
<div class="container">
        <div class="row">
        <div class="col-sm-8">
            <div class="row video-section meta-maxwidth-230">
<?php 

$args = array(
    'post_type'         => array( 'video' ),
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$author_videos = new WP_Query( $args );

if ( $author_videos->have_posts() ) : while ( $author_videos->have_posts() ) : $author_videos->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <?php do_action( 'mars_pagination', null );?>
    <?php wp_reset_postdata();
    endif; ?>
        </div>
    </div>
</div>
</div>  
    <?php }

So I figured out how to show the pagination, with the following code. The problem is when I click on the next page, it brings me to a 404 page. But when I manually visit a custom URL it does show the second page correctly, here are both examples:

404 Version:

This is the url structure that is generated by this pagination code

www.website.com/members/username/my-videos/page/2/

Working Version:

This is a manual url I found to be working

www.website.com/members/username/my-videos/?page=2

Question is, how to get it to work with the first version (/page/2/)

CODE:

    <?php function my_videos_screen_content() { 
echo 'We are currently working on this section, some content may not appear properly. Thank you for your patience.'; ?>
<div class="container">
        <div class="row">
        <div class="col-sm-8">
            <div class="row video-section meta-maxwidth-230">
<?php 

$custom_query_args = array(
    'post_type'         => 'video',
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$custom_query = new WP_Query( $custom_query_args );

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <?php endif; 
    wp_reset_postdata(); ?>

    <?php // Custom query loop pagination
    previous_posts_link( 'Newer Videos' );
    next_posts_link( 'Older Videos', $custom_query->max_num_pages );?>

    <?php // Reset main query object
    $wp_query = NULL;
    $wp_query = $temp_query; ?>

        </div>
    </div><!-- /.row -->
</div>
</div><!-- /.container -->   

Share Improve this question edited Nov 22, 2017 at 19:19 Peter asked Nov 21, 2017 at 23:42 PeterPeter 13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

This may not be the proper way but it works in BP profile pages

    ...

    $paged = ( isset( $_GET['vp'] ) ) ? $_GET['vp'] : 1;

    $args = array(
        'post_type'         => array( 'video' ),
        'author'            => bp_displayed_user_id(),
        'paged'             => $paged,
        'posts_per_page'    => get_option( 'posts_per_page' )
        );

    $author_videos = new WP_Query( $args );

    if ( $author_videos->have_posts() ) : while ( $author_videos->have_posts() ) : $author_videos->the_post();
        get_template_part( 'loop', 'video' );
        endwhile; ?>
        </div>

        <div class="entry-content"><br>
            <?php echo videos_profile_pagination( $wp_query ); ?>
        </div>

        <?php  wp_reset_query(); 
        endif; ?>
            </div>
        </div>
    </div>
    </div>  
 <?php }

// pagination for profile video loop page
function videos_profile_pagination( $wp_query ) {

    $videos_profile_page_links = paginate_links( array(
        'base' => esc_url( add_query_arg( 'vp', '%#%' ) ),
        'format' => '',
        'total' => ceil( (int) $wp_query->found_posts / (int) get_query_var('posts_per_page') ),
        'current' => (int) get_query_var('paged'),
    ) );

    return apply_filters( 'videos_profile_pagination', $videos_profile_page_links );

}

$paged = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;

$custom_query_args = array(
    'post_type'         => 'video',
    'paged'             => $paged,
    'author'            => bp_displayed_user_id(),
    'posts_per_page'    => get_option( 'posts_per_page' )
    );

$custom_query = new WP_Query( $custom_query_args );

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
    get_template_part( 'loop', 'video' );
    endwhile; ?>
    </div>
    <ul class="pagination">
    <?php
    // echo videos_profile_pagination( $wp_query ); 
    $list = videos_profile_pagination($custom_query);
     foreach ( $list as $page ) {

                echo "<li>$page</li>";

        }
    ?>
    </ul>
    <?php
    add_action('mars_pagination', 'mars_pagination', 10, 1);
    wp_reset_query(); 

    endif; 
?>
    <?php // Reset main query object
   $wp_query = NULL;
    $wp_query = $temp_query; ?>

        </div>
    </div><!-- /.row -->
</div>
</div><!-- /.container -->   

pagination for profile video loop page

function videos_profile_pagination( $wp_query ) {

        $query = $wp_query;

    if ( $query->max_num_pages < 2 ) {

        return;

    }   

        $paged        = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
    // Set up paginated links.

    $links = paginate_links( array(

        'base'               => '%_%',
'format'             => '?page=%#%',

        'total'    => $query->max_num_pages,

        'current'  => $paged,

        'mid_size' => 3,

        'type'  =>  'array',



        'prev_next'    => true,

        'prev_text' => !is_rtl() ? __( '&larr; Previous ', 'mars' ) : __( ' &rarr; Previous', 'mars' ),

        'next_text' => !is_rtl() ? __( 'Next &rarr;', 'mars' ) : __( 'Next &larr;', 'mars' )

    ) );

return $links;

}

本文标签: How to add pagination to a post loop in a custom BuddyPress tab