php - Wordpress 自定义帖子类型分页 - /page/2/ 未找到
问题描述
这真让我抓狂。我已经尝试了所有我能找到的解决方案,但仍然不适合我。
我有一个自定义帖子类型。
/**
* Videos Custom Post Type
*/
function videos_post_type() {
register_post_type( 'videos', array(
'labels' => array(
'name' => 'Videos',
'singular_name' => 'Video',
'add_new_item' => 'Add New Video',
'add_new' => 'Add New Video',
'edit_item' => 'Edit Video',
'new_item' => 'New Video',
'all_items' => 'All Videos'
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'videos' ),
'menu_icon' => 'dashicons-format-video'
));
}
add_action( 'init', 'videos_post_type' );
除了“存档”页面上的分页外,一切正常。分页链接正确显示,但是当我单击“下一步”时,它以找不到页面错误结束。
<div class="ast-row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'videos',
'posts_per_page' => 1,
'orderby'=> 'menu_order',
'paged'=> $paged
) );
while ( $loop->have_posts() ) {
$loop->the_post(); ?>
<div class="ast-col-md-4">
<?php echo the_content(); ?>
</div>
<?php } ?>
</div>
<div class="ast-row">
<?php
echo paginate_links( array(
'total' => $loop->max_num_pages
) );
?>
<?php wp_reset_postdata(); ?>
</div>
还尝试了以下 echo_paginate_links()
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
此外- * 我试图保存永久链接 * 我试图更改永久链接结构 * 我尝试过 query_posts 而不是 WP_Query ---这些都不起作用。
我还应该提到我还没有创建一个 single-videos.php 模板(因为我不需要它来处理这种帖子类型),但我认为这与这个问题没有任何关系。如果我错了,请纠正我。我只是无法理解它。如果有帮助,我正在使用 Astra 主题。
请分享您的建议。谢谢!
解决方案
试试下面(替换你的html)
<?php
global $loop;
$query = $query ? $query : $loop;
$big = 999999999;
$paginate = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'type' => 'array',
'total' => $query->max_num_pages,
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'prev_previous' =>('← Older posts'),
'prev_next' => ( 'Newer posts →' ),
'prev_text' => __('«'),
'next_text' => __('»'),
));
if ($query->max_num_pages > 1) :
?>
<div class="paginate">
<ul class="pagination">
<?php
foreach ( $paginate as $page ) {
echo '<li>' . $page . '</li>';
}
?>
</ul>
</div>
<?php endif; ?>
推荐阅读
- sql - 多重连接不适用于两个字符串属性
- node.js - 如何读取未定义的属性“订阅”?
- javascript - CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the first request to the route
- spring-boot - 当服务重新启动时,如何确保 Spring Integration poller 工作并执行错过的计划?
- sql - 如何在 SQL 中使用给定条件重置最大序列号
- python - 从 WebElement 文本中删除特定单词
- javascript - 在反应中使用自定义钩子数组
- c# - 如何将样式从模板复制到另一个文档
- python - 查找数百万条记录的 Pearson 相关性
- javascript - 如何将现有对象修改为新对象javascript