可能是一个相当直截了当的问题。
我的“主页”不是博客页面。我有一个page-template
已命名page-knowledge.php
上面有以下循环。
<?php
echo \'<ul class="blog-items">\';
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 5,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
get_template_part( \'inc/post\', \'item\' );
endwhile;
wp_reset_postdata();
echo \'</ul>\';
?>
因此,此页面查询
5
普通岗位的。我怎样才能在这个页面上添加分页,这样我就可以像你所期望的那样跳过其余的帖子。
<小时>
UPDATE
<?php
echo \'<ul class="blog-items">\';
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 5,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
get_template_part( \'inc/post\', \'item\' );
endwhile;
wp_reset_postdata();
echo \'</ul>\';
?>
<div class="pagination wrapper">
<?php
$endsize = $midsize = 1;
$type = \'plain\';
$loop->query_vars[\'paged\'] > 1 ? $current = $loop->query_vars[\'paged\'] : $current = 1;
// Sanitize input argument values
if ( ! in_array( $type, array( \'plain\', \'list\', \'array\' ) ) ) $type = \'plain\';
$endsize = (int) $endsize;
$midsize = (int) $midsize;
// Setup argument array for paginate_links()
$pagination = array(
\'base\' => @add_query_arg(\'paged\',\'%#%\'),
\'total\' => $loop->max_num_pages,
\'current\' => $current,
\'show_all\' => false,
\'end_size\' => $endsize,
\'mid_size\' => $midsize,
\'type\' => $type,
\'prev_next\' => false,
\'paged\' => $loop->query_vars[\'paged\']
);
echo paginate_links( $pagination );
?>
</div>
最合适的回答,由SO网友:Tom J Nowell 整理而成
get_pagination_links
正在查看$wp_query
, 而您希望为名为$loop
. 因此,使用paginate_links
类似于此代码的内容,它基于get_pagination_links
, 为循环查询对象交换了带有wp查询的alebit:
$endsize = $midsize = 1;
$type = \'plain\';
$loop->query_vars[\'paged\'] > 1 ? $current = $loop->query_vars[\'paged\'] : $current = 1;
// Sanitize input argument values
if ( ! in_array( $type, array( \'plain\', \'list\', \'array\' ) ) ) $type = \'plain\';
$endsize = (int) $endsize;
$midsize = (int) $midsize;
// Setup argument array for paginate_links()
$pagination = array(
\'base\' => @add_query_arg(\'paged\',\'%#%\'),
\'total\' => $loop->max_num_pages,
\'current\' => $current,
\'show_all\' => false,
\'end_size\' => $endsize,
\'mid_size\' => $midsize,
\'type\' => $type,
\'prev_next\' => false,
\'paged\' => $loop->query_vars[\'paged\']
);
echo paginate_links( $pagination );
最后你需要抓住
paged
URL变量通过$\\u GET,进行一次小的清理(检查它是一个数字并且设置好了),然后将其放入
WP_Query
args数组