【发布时间】:2018-10-05 05:04:25
【问题描述】:
我有一个自定义帖子类型“charters”,在我的 category.php 页面中,我有一个循环为当前类别 ID 拉入“charter”帖子。当我单击第 2 页链接时,我有一个显示页码的自定义分页设置,它转到显示 404 错误的 URL /page/2。我不知道为什么会收到 404。如果我调整每页的帖子,分页会更新并显示正确的页数,但链接不显示。
Category.php 代码:
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$product_args = array(
'post_type' => 'charters',
'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
'paged' => $paged,
'page' => $paged,
'cat' => $cat
);
$product_query = new WP_Query( $product_args ); ?>
<?php if ( $product_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
<article class="loop">
<h3><?php the_title(); ?></h3>
<div class="content">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists( 'custom_pagination' )) :
custom_pagination( $product_query->max_num_pages,"",$paged );
endif;
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Functions.php 代码:
function prefix_change_cpt_archive_per_page( $query ) {
//* for cpt or any post type main archive
if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );
function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$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>";
}
我的永久链接设置设置为“帖子名称”我感觉这与重写有关,但我不知道如何让页面 URL 正常工作。我的类别示例如下:
/类别/长滩/
/category/san-diego/
【问题讨论】:
-
嗨,您可以在
$args中添加一些选项,例如$args = array( 'post_type' => 'charters', 'cat'=> $cat, 'posts_per_page' => 10, 'offset' => 0, );以获得前 10 个帖子。您可以调整offset以获取下一组帖子。 -
你能指出如何调整偏移的正确方向吗?
-
您可以在加载更多按钮中添加偏移量,例如
<button class="loadmore" data-offset=10></button>然后您可以使用该偏移量调用 ajax 到 php 文件,在该 php 文件上您可以使用该偏移量并生成一个新的WP_Query,您可以返回该查询的结果并将其附加到您的第 10 个帖子的末尾。 -
啊伙计,这听起来是个不错的方法,但不幸的是我不知道该怎么做。需要一些文件。
-
我已对原始代码/问题进行了更改,请参见上文。
标签: php wordpress wordpress-theming custom-wordpress-pages