我正在为WordPress中的自定义模板创建分页,并在单击除第1页之外的所有其他页面时继续获取404.我能够成功获取每个页面的分页链接,但如上所述,不断返回404。
这是我要求分页的函数,它存在于functions.php中:
function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
$bignum = 999999999;
if ( $totalpages <= 1 || $page > $totalpages ) return;
//NOTE: https://codex.wordpress.org/Function_Reference/paginate_links
return paginate_links( array(
'total' => $totalpages,
'prev_next' => false,
'type' => 'list',
'show_all' => false,
'end_size' => $end_size,
'mid_size' => $mid_size
) );
}
以下是生活在taxonomy-pagename.php中的代码:
<?php
$this_page_taxonomy = $wp_query->get_queried_object(); //getting this page terms
$current_slug = $this_page_taxonomy->slug; //current slug
$go_cpt = 'hope_go'; //taxonony name
$number = 1; // number of terms to display per page
// Setup for pagination:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged > 0) ? $number * ($paged - 1) : 1;
$totalposts = $this_page_taxonomy->count;
$totalpages = ceil($totalposts / $number);
$hope_go_args = array(
'post_type' => $go_cpt,
'orderby' => 'meta_value',
'meta_key' => 'hope_go_series_date',
'meta_value' => $time,
'meta_compare' => '>=',
'paged' => $paged,
'order' => 'ASC',
'posts_per_page' => $number,
'hierarchical' => true,
'offset' => $offset,
'tax_query' => array(
array(
'taxonomy' => 'hope_go_series',
'field' => 'slug',
'terms' => $current_slug,
) ,
)
);
$hope_go_recent_posts = new WP_Query($hope_go_args);
if ($hope_go_recent_posts->have_posts()):
while ($hope_go_recent_posts->have_posts()):
$hope_go_recent_posts->the_post();
printf('<nav class="recent-series__pagination">%s</nav>',
custom_page_navi($totalpages, $paged, 3, 0) < ? php
//Content would go here
endwhile;
?>
<?php
else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?> </p>
<?php
endif;
wp_reset_postdata();
?>
我尝试了重写,但这不是问题。似乎除了第1页之外的所有页面都不能引入相应的帖子。任何有关这方面的见解将不胜感激!
答案 0 :(得分:0)
请按照以下步骤进行分页
只需在模板 functions.php 中添加以下代码,或在需要分页的地方添加脚本。
<强>步骤1 强>
$customPagHTML = "";
$query = "SELECT * FROM custom_table";
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$items_per_page = 4;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$result = $wpdb->get_results( $query . " ORDER BY field DESC LIMIT ${offset}, ${items_per_page}" );
$totalPage = ceil($total / $items_per_page);
然后上面的脚本有“custom_table”你需要用你的表名替换你也可以选择添加ORDER BY或GROUP BY等。然后只需迭代你的查询结果的“$ result”
<强>步骤-2 强>
if($totalPage > 1){
$customPagHTML = '<div><span>Page '.$page.' of '.$totalPage.'</span>'.paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => $totalPage,
'current' => $page
)).'</div>';
}
<强>步骤-3 强>
echo $customPagHTML;
然后只需输出$ customPagHTML变量,就像上面那样需要分页。