【发布时间】:2017-06-30 13:32:48
【问题描述】:
美好的一天!问题是这样的:在模板类别(存档)中,当您单击 404 错误的第 2 页时,分页不起作用。 不明白怎么解决的请帮忙,头都坏了
我的循环:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$arg = array(
'cat' => get_queried_object_id(),
'post_type'=>'post',
'posts_per_page'=>9,
//'order'=>'desc',
'paged' => $paged,
);
$query = new WP_query($arg);
if($query->have_posts()) : ?>
<section class="blog">
<?php
echo '<div class="row">';
$i=0;
$formcreated=false;
while( $query->have_posts() ) :
$query->the_post();
// display post
endwhile;
wp_reset_postdata();
endif;
?>
<div class="pagination">
<?php
if (function_exists('custom_pagination')) {
custom_pagination($query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
</div>
还有我的自定义分页:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_category()){
$query->set('posts_per_page', 1);
$query->set('post_type','product');
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$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,
'prev_text' => __('<'),
'next_text' => __('>'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo $paginate_links;
echo "</nav>";
}
}
【问题讨论】:
-
@Christina 在我未经训练的眼睛看来,目前的 OP 已经在我们在这里看到的代码中至少部分地使用了这个技巧?
-
他在第二组代码中每页有 1 个帖子,大概是在 functions.php 中,并且他在循环参数上每页有 9 个帖子。你看到数字不匹配。此外,他希望所有类别都具有相同的分页,还是只是自定义循环中使用的一个?所以他需要使用比 is_category 更具体的条件,并且检查它是否不是内置的(cpt)不是你这样做的方式。
-
好的。我明白他的意思。当阅读设置中的每页帖子大于自定义查询中的帖子时,您会得到 404。我将在今天晚些时候回答这个问题。
标签: php wordpress pagination wordpress-theming custom-wordpress-pages