当我在自定义帖子类型循环中调用该函数时,我的自定义分页功能未返回任何值。
我的分页功能适用于主页,但不适用于自定义帖子类型,我也将其用于WooCommerce分页,并且在这里工作正常。
这是我的循环:
<?php
$args = array(
'post_type'=>'winners',
'posts_per_page' => 6,
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
topdraw_pagination();
}
wp_reset_postdata();
?>
这是我的分页功能:
<?php
function topdraw_pagination() {
if( is_singular() ) {
return;
}
global $wp_query;
if( $wp_query->max_num_pages <= 1 ) {
return;
}
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
if( $paged >= 1 ) {
$links[] = $paged;
}
if( $paged >= 3 ) {
$links[] = $paged - 1;
}
if( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 1;
}
echo '<nav class="pagination">' . "\n";
if ( get_previous_posts_link() ) {
printf( '%s' . "\n", get_previous_posts_link('<span class="icon is-small"><i class="fas fa-caret-left"></i></span>') );
}
if ( get_next_posts_link() ) {
printf( '%s' . "\n", get_next_posts_link('<span class="icon is-small"><i class="fas fa-caret-right"></i></span>') );
}
echo '<ul class="pagination-list">' . "\n";
if( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' is-current' : '';
$aria_current = 1 == $paged ? ' aria-current="page"' : '';
printf( '<li><a href="%s" class="pagination-link%s" aria-label="Goto page 1"%s>%s</a></li>' . "\n", esc_url( get_pagenum_link( 1 ) ), $class, $aria_current, '1' );
if ( ! in_array( 2, $links ) ) {
echo '<li><span class="pagination-ellipsis">…</span></li>' . "\n";
}
}
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' is-current' : '';
$aria_current = $paged == $link ? ' aria-current="page"' : '';
printf( '<li><a href="%s" class="pagination-link%s" aria-label="Goto page %s"%s>%s</a></li>' . "\n", esc_url( get_pagenum_link( $link ) ), $class, $link, $aria_current, $link );
}
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) ) {
echo '<li><span class="pagination-ellipsis">…</span></li>' . "\n";
}
$class = $paged == $max ? ' is-current' : '';
$aria_current = $paged == $max ? ' aria-current="page"' : '';
printf( '<li><a href="%s" class="pagination-link%s" aria-label="Goto page %s"%s>%s</a></li>' . "\n", esc_url( get_pagenum_link( $max ) ), $class, $max, $aria_current, $max );
}
echo '</ul></nav>' . "\n";
}
add_filter( 'next_posts_link_attributes', 'topdraw_next_posts_link_class' );
add_filter( 'previous_posts_link_attributes', 'topdraw_previous_posts_link_class' );
function topdraw_next_posts_link_class() {
return 'class="pagination-next"';
}
function topdraw_previous_posts_link_class() {
return 'class="pagination-previous"';
}
谢谢您的帮助。
答案 0 :(得分:0)
您需要将自定义WP_Query实例传递给分页函数,而不是使用全局$wp_query
$args = array(
'post_type'=>'winners',
'posts_per_page' => 6,
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
the_title(); ?>
endwhile;
topdraw_pagination( $loop );
}
wp_reset_postdata();
还有您的功能
function topdraw_pagination( $loop ) {
if( is_singular() ) {
return;
}
if( $loop->max_num_pages <= 1 ) {
return;
}
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $loop->max_num_pages );
if( $paged >= 1 ) {
$links[] = $paged;
}
if( $paged >= 3 ) {
$links[] = $paged - 1;
}
if( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 1;
}
echo '<nav class="pagination">' . "\n";
if ( get_previous_posts_link() ) {
printf( '%s' . "\n", get_previous_posts_link('<span class="icon is-small"><i class="fas fa-caret-left"></i></span>') );
}
if ( get_next_posts_link() ) {
printf( '%s' . "\n", get_next_posts_link('<span class="icon is-small"><i class="fas fa-caret-right"></i></span>') );
}
echo '<ul class="pagination-list">' . "\n";
if( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' is-current' : '';
$aria_current = 1 == $paged ? ' aria-current="page"' : '';
printf( '<li><a href="%s" class="pagination-link%s" aria-label="Goto page 1"%s>%s</a></li>' . "\n", esc_url( get_pagenum_link( 1 ) ), $class, $aria_current, '1' );
if ( ! in_array( 2, $links ) ) {
echo '<li><span class="pagination-ellipsis">…</span></li>' . "\n";
}
}
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' is-current' : '';
$aria_current = $paged == $link ? ' aria-current="page"' : '';
printf( '<li><a href="%s" class="pagination-link%s" aria-label="Goto page %s"%s>%s</a></li>' . "\n", esc_url( get_pagenum_link( $link ) ), $class, $link, $aria_current, $link );
}
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) ) {
echo '<li><span class="pagination-ellipsis">…</span></li>' . "\n";
}
$class = $paged == $max ? ' is-current' : '';
$aria_current = $paged == $max ? ' aria-current="page"' : '';
printf( '<li><a href="%s" class="pagination-link%s" aria-label="Goto page %s"%s>%s</a></li>' . "\n", esc_url( get_pagenum_link( $max ) ), $class, $max, $aria_current, $max );
}
echo '</ul></nav>' . "\n";
}
答案 1 :(得分:0)
我通过在自定义帖子类型循环中将$ loop重命名为$ wp_query来解决此问题。如果有人能解释为什么这可以解决问题,我将不胜感激。