如何在自定义BuddyPress选项卡中向POST循环添加分页

时间:2017-11-21 作者:Peter

我在BuddyPress个人资料页面中创建了一个名为“视频”的新选项卡。此选项卡包含该用户添加的所有视频帖子。问题是它只显示前12篇文章,而分页没有显示出来。我确实尝试包含主题提供的分页代码,但没有成功。

备注:

我正在使用一个名为“VideoTube”的主题

要分页的post循环来自称为“视频”的自定义post类型

主题提供的原始分页代码为<?php do_action( \'mars_pagination\', null );?>

下面的代码是从我的函数中提取的。子主题文件夹中的php文件。

提前感谢您的帮助:D

代码:

    <?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 }
因此,我想出了如何使用以下代码显示分页。问题是,当我单击下一页时,它会将我带到404页。但当我手动访问自定义URL时,它确实会正确显示第二个页面,以下是两个示例:

404 Version:

这是此分页代码生成的url结构

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

Working Version:

这是我发现正在使用的手动url

www.website。com/成员/用户名/我的视频/?页码=2

问题是,如何让它与第一个版本配合使用(/第/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 -->   

2 个回复
SO网友:shanebp

这可能不是正确的方法,但在BP个人资料页面中有效

    ...

    $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 );

}

SO网友:sanjay kumar

$paged=(isset($\\u GET[\'page]))$_获取[\'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 -->   
配置文件视频循环页面分页

函数videos\\u profile\\u分页($wp\\u 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;
}

结束

相关推荐

Need help with pagination

我在计算分页时遇到了一个问题。使用我在这里找到的信息Wordpress Pagination Not Working - Always Showing First Pages Content 线程,我已成功在页面上设置分页。代码如下所示:<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; $args = array( \'posts_per_page\' => 10