Wordpress 自定义 Post 类型:分页在每个页面上重新加载相同的内容
Wordpress Custom Post Type: Pagination reloads same content on every page
所以我有一个名为 broadcast
的自定义 post 类型,它目前将 10 post 页面吐出到存档页面上。但是,当你通过分页切换页面时,URL变成了page/2/
并且底部的当前按钮发生了变化,但是同样的10个post保持不变。所以事情的分页方面似乎在工作,它只是没有在不同的 posts 中加载。这是代码(这可能太多了,但上下文越多越好):
<?php
// WP_Query arguments
$args = array(
'post_type' => array('broadcast'),
'post_status' => array('publish'),
'nopaging' => false,
'offset' => 1,
'order' => 'DESC',
'orderby' => 'date',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'format',
'terms' => 'videos',
'field' => 'slug'
)
)
);
// The Query
$broadcasts = new WP_Query($args);
// The Loop
if ($broadcasts->have_posts()) {
while ($broadcasts->have_posts()) {
$broadcasts->the_post(); ?>
// SHOW THE POSTS HERE
<?php } ?>
<!-- Start Pagination-->
<div class="col-span-12 p-5 mb-8 pagination text-center">
<?php
echo paginate_links(array(
'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
'total' => $broadcasts->max_num_pages,
'current' => max(1, get_query_var('paged')),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf('<i></i> %1$s', __('Newer Videos', 'text-domain')),
'next_text' => sprintf('%1$s <i></i>', __('Older Videos', 'text-domain')),
'add_args' => false,
'add_fragment' => '',
));
?>
</div>
<?php
} else {
echo 'there are no posts.'; // no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
感谢任何帮助,我没有阅读文档,因为这 * 应该 * 工作并且它不会抛出错误让我调试。
谢谢!
您的问题的原因是偏移参数。
当设置偏移参数时,分页参数将被忽略。
检查 WP_Query 个参数。
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
offset (int) – number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
我认为您正试图跳过最新的 post。
在这种情况下,您可以使用以下代码。
$latest_post_args = array(
'post_type' => 'broadcast',
'numberposts' => 1,
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'format',
'terms' => 'videos',
'field' => 'slug'
)
)
);
$latest_post_ids = get_posts( $latest_post_args );
$args = array(
'post_type' => array('broadcast'),
'post_status' => array('publish'),
'nopaging' => false,
'post__not_in' => $latest_post_ids,
'order' => 'DESC',
'orderby' => 'date',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'format',
'terms' => 'videos',
'field' => 'slug'
)
)
);
所以我有一个名为 broadcast
的自定义 post 类型,它目前将 10 post 页面吐出到存档页面上。但是,当你通过分页切换页面时,URL变成了page/2/
并且底部的当前按钮发生了变化,但是同样的10个post保持不变。所以事情的分页方面似乎在工作,它只是没有在不同的 posts 中加载。这是代码(这可能太多了,但上下文越多越好):
<?php
// WP_Query arguments
$args = array(
'post_type' => array('broadcast'),
'post_status' => array('publish'),
'nopaging' => false,
'offset' => 1,
'order' => 'DESC',
'orderby' => 'date',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'format',
'terms' => 'videos',
'field' => 'slug'
)
)
);
// The Query
$broadcasts = new WP_Query($args);
// The Loop
if ($broadcasts->have_posts()) {
while ($broadcasts->have_posts()) {
$broadcasts->the_post(); ?>
// SHOW THE POSTS HERE
<?php } ?>
<!-- Start Pagination-->
<div class="col-span-12 p-5 mb-8 pagination text-center">
<?php
echo paginate_links(array(
'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
'total' => $broadcasts->max_num_pages,
'current' => max(1, get_query_var('paged')),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf('<i></i> %1$s', __('Newer Videos', 'text-domain')),
'next_text' => sprintf('%1$s <i></i>', __('Older Videos', 'text-domain')),
'add_args' => false,
'add_fragment' => '',
));
?>
</div>
<?php
} else {
echo 'there are no posts.'; // no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
感谢任何帮助,我没有阅读文档,因为这 * 应该 * 工作并且它不会抛出错误让我调试。 谢谢!
您的问题的原因是偏移参数。
当设置偏移参数时,分页参数将被忽略。
检查 WP_Query 个参数。
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
offset (int) – number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
我认为您正试图跳过最新的 post。
在这种情况下,您可以使用以下代码。
$latest_post_args = array(
'post_type' => 'broadcast',
'numberposts' => 1,
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'format',
'terms' => 'videos',
'field' => 'slug'
)
)
);
$latest_post_ids = get_posts( $latest_post_args );
$args = array(
'post_type' => array('broadcast'),
'post_status' => array('publish'),
'nopaging' => false,
'post__not_in' => $latest_post_ids,
'order' => 'DESC',
'orderby' => 'date',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'format',
'terms' => 'videos',
'field' => 'slug'
)
)
);