Php Wordpress分页-Can';不要转到其他页面
我正在使用wordpress,并尝试使用wordpress WP_查询创建分页。我使用了一个教程,有些人和我一样有同样的问题,他们无法连接到分页的第二或第三页。我的数据库中有11篇文章,我将它们拆分,每页显示4篇,无论我尝试转到第二页还是第三页,都只显示“404页未找到”。这是我的index.php:Php Wordpress分页-Can';不要转到其他页面,php,wordpress,pagination,http-status-code-404,Php,Wordpress,Pagination,Http Status Code 404,我正在使用wordpress,并尝试使用wordpress WP_查询创建分页。我使用了一个教程,有些人和我一样有同样的问题,他们无法连接到分页的第二或第三页。我的数据库中有11篇文章,我将它们拆分,每页显示4篇,无论我尝试转到第二页还是第三页,都只显示“404页未找到”。这是我的index.php: <?php get_header();?> <div id="primary" class="content-area"> <main id=
<?php
get_header();?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'all_products',
'posts_per_page' => 4,
'paged' => $paged,
'page' => $paged
);
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<article class="loop">
<div class="product">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<ul>
<li><strong>Description:</strong> <?php echo(types_render_field( "description_of_the_product", array( 'raw' => true) )); ?></li>
<li><strong>Type:</strong> <?php echo(types_render_field( "type", array( 'raw' => true) )); ?></li>
<li><strong>Price:</strong> <?php echo(types_render_field( "price", array( 'raw' => true) )); ?></li>
<li><strong>Manufacturer:</strong> <?php echo(types_render_field( "manufacturer", array( 'raw' => true) )); ?></li>
<li><strong>Availability:</strong> <?php echo(types_render_field( "availability:", array( 'raw' => true) )); ?></li>
<li><strong>Reference:</strong> <?php echo(types_render_field( "reference", array( 'raw' => true) )); ?></li>
<a href="<?php the_permalink(); ?>">Read more</a>
<ul>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists('custom_pagination')) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
?>
<?php next_posts_link( $label , $max_pages ); ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php get_footer(); ?>
- 说明:
- 类型:
- 价格:
- 制造商:
- 可用性:
- 参考资料:
非常感谢您的帮助
分页功能(自定义分页):
函数自定义分页($numpages='',$pagerange='',$paged=''){
if(空($pagerange)){
$pagerange=2;
}
/**
*我们功能的第一部分是回退
*用于常规循环内的自定义分页,该循环
*使用全局$paged和全局$wp_查询变量。
*
*这很好,因为我们现在可以覆盖默认分页
*并在默认查询中使用此函数
*和自定义查询。
*/
全球$paged;
如果(空($paged)){
$paged=1;
}
如果($numpages==''){
全局$wp_查询;
$numpages=$wp\u query->max\u num\u页面;
如果(!$numpages){
$numpages=1;
}
}
/**
*我们构造分页参数以进入分页链接
*功能。
*/
$pagination_args=数组(
'base'=>get_pagenum_link(1)。'%\%',
'格式'=>'第/%#%'页,
“总计”=>$numpages,
“当前”=>$paged,
“全部显示”=>False,
“结束大小”=>1,
“中等大小”=>$pagerange,
“上一步”=>正确,
“上一个文本”=>“«;”,
“下一个文本”=>“»;”,
'类型'=>'普通',
'add_args'=>false,
“添加片段”=>“
);
$paginate_links=paginate_links($pagination_args);
如果($paginate_链接){
回声“;
呼应“$numpages.”中的“$paged.”页;
echo$paginate_链接;
回声“;
}
}
请发布您的分页函数自定义分页
。该函数现在正在发布中。为了测试目的,请尝试减少分页参数。以文档中的示例为例:。您是否在页面上列出了设置为静态frontpage的帖子?如果没有,您不需要在WP_查询库中输入参数页面
,即可获得答案。我在主页上显示我的帖子,我已经尝试了你的解决方案,但仍然不起作用。
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 "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}