如何在wordpress自定义模板页面中使用作者特定的帖子实现分页

时间:2018-04-05 06:35:37

标签: php mysql wordpress

我想在自定义模板页面上列出当前登录作者的帖子,其下方有分页,其中包含各个页面链接的编号,在上方有一个下拉列表,我们将从中选择将显示的帖子数量那个页面(这个工作正常)但是当我尝试在下方分页时转到第二页链接它将转到第2页但总是显示相同的帖子。 Image view of template page

我已经完成了一些搜索并在代码下面实现了 表单显示下拉列表



<form name="frm" class="db_posts_per_page_form" method="post" action="">
						<label for="db_posts_per_page">Posts per page</label>
						<select onchange="document.frm.submit()" name="db_posts_per_page" id="db_posts_per_page">
							<option value="2" <?php selected(2,$_REQUEST['db_posts_per_page']);?>>2</option>
							<option value="4" <?php selected(4,$_REQUEST['db_posts_per_page']);?>>4</option>
							<option value="6" <?php selected(6,$_REQUEST['db_posts_per_page']);?>>6</option>
							<option value="8" <?php selected(8,$_REQUEST['db_posts_per_page']);?>>8</option>
							<option value="10" <?php selected(10,$_REQUEST['db_posts_per_page']);?>>10</option>
						</select>
					</form>
&#13;
&#13;
&#13;

和php for pagination

global $current_user;
                        if( isset($_POST['db_posts_per_page'] )):
                            $posts_per_page = $_POST['db_posts_per_page'];
                        else:
                            $posts_per_page = 2;
                        endif;
                        $author_query = array('posts_per_page' => $posts_per_page,'author' => $current_user->ID, 'post_status' => array('publish', 'pending', 'draft'));
                        $author_posts = new WP_Query($author_query);
                        while($author_posts->have_posts()) : $author_posts->the_post();
                            get_template_part( 'parts/post/content', 'blog-list' );
                        endwhile;
                        //global $author_posts;
                        $big = 999999999;
                        $paginate_links =  paginate_links ( array(
                        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                        'format' => '?paged=%#%',
                        'current' => max( 1, get_query_var('paged') ),
                        'total' => $author_posts->max_num_pages,
                        'mid_size' => 5,
                        'prev_next' => true,
                        ) );
                         if (!empty($paginate_links)) : ?>
                            <div class="pagignation"><?php echo $paginate_links;?></div>
                        <?php endif; 

1 个答案:

答案 0 :(得分:0)

我已将$ author_query更改为$author_query = new WP_Query( array('posts_per_page'=>$posts_per_page, 'post_type'=>'post', 'author' => $current_user->ID, 'post_status' => array('publish', 'pending', 'draft'), 'paged' => get_query_var('paged') ? get_query_var('paged') : 1));并添加了wp_reset_postdata();在回显$ paginate_links

之前

所以我的自定义模板页面的全部代码现在如下所示。

<div id="primary" class="content-area">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-md-12">
                <h1 class="page-title"><?php the_title();?></h1>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="row add-new">
            <div class="col-xs-12 col-md-6">
                <form name="frm" class="db_posts_per_page_form" method="get" action="">
                    <label for="db_posts_per_page">Posts per page</label>
                    <select onchange="document.frm.submit()" name="db_posts_per_page" id="db_posts_per_page">
                        <option value="2" <?php selected(2,$_REQUEST['db_posts_per_page']);?>>2</option>
                        <option value="4" <?php selected(4,$_REQUEST['db_posts_per_page']);?>>4</option>
                        <option value="6" <?php selected(6,$_REQUEST['db_posts_per_page']);?>>6</option>
                        <option value="8" <?php selected(8,$_REQUEST['db_posts_per_page']);?>>8</option>
                        <option value="10" <?php selected(10,$_REQUEST['db_posts_per_page']);?>>10</option>
                    </select>
                </form>
            </div>
            <div class="col-xs-12 col-md-6">
                <div class="pull-right col-xs-12 col-md-3"><a class="btn btn-primary" href="<?php echo esc_url( get_permalink(80) ); ?>">Add New Posts</a></div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 col-md-4">
                <h5><u>Title</u></h5>
            </div>
            <div class="col-xs-12 col-md-2">
                <h5><u>Author</u></h5>
            </div>
            <div class="col-xs-12 col-md-2">
                <h5><u>Status</u></h5>
            </div>
            <div class="col-xs-12 col-md-2">
                <h5><u>Posted On</u></h5>
            </div>
            <div class="col-xs-12 col-md-2">
                <h5><u>Actions</u></h5>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 col-md-12">
                <?php if ( is_user_logged_in() ):
                        global $current_user;
                        if( isset($_GET['db_posts_per_page'] )):
                            $posts_per_page = $_GET['db_posts_per_page'];
                        else:
                            $posts_per_page = 2;
                        endif;
                        //echo $posts_per_page;

                        $author_query = new WP_Query( array('posts_per_page'=>$posts_per_page,
                             'post_type'=>'post',
                             'author' => $current_user->ID,
                             'post_status' => array('publish', 'pending', 'draft'),
                             'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                        ); 

                        //print_r($author_query);
                        while($author_query->have_posts()) : $author_query->the_post();
                            get_template_part( 'parts/post/content', 'blog-list' );
                        endwhile;

                        $big = 999999999;
                        $paginate_links =  paginate_links ( array(
                        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                        'format' => '?paged=%#%',
                        'current' => max( 1, get_query_var('paged') ),
                        'total' => $author_query->max_num_pages,
                        'mid_size' => 5,
                        'prev_next' => true,
                        ) );
                        wp_reset_postdata();
                         if (!empty($paginate_links)) : ?>
                            <div class="pagignation"><?php echo $paginate_links;?></div>
                        <?php endif; 
                      else:
                        echo 'You need to be logged in to see your posts.';
                endif;?>
            </div>

        </div>
    </div>
</div>