自定义帖子类型页面不显示自定义分页

问题描述 投票:0回答:1

我使用了代码片段来构建自定义分页:

<?php
if ( ! function_exists( 'procare_paging_nav' ) ) :
/**
 * Display navigation to next/previous set of posts when applicable.
 *
 * @return void
 */
function procare_paging_nav() {
    // Don't print empty markup if there's only one page.
    if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
        return;
    }

    $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( '?', $pagenum_link );

    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }

    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

    $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

    // Set up paginated links.
    $links = paginate_links( array(
        'base'     => $pagenum_link,
        'format'   => $format,
        'total'    => $GLOBALS['wp_query']->max_num_pages,
        'current'  => $paged,
        'mid_size' => 2,
        'add_args' => array_map( 'urlencode', $query_args ),
        'prev_text' => '<i class="fa fa-chevron-left"></i>',
        'next_text' => '<i class="fa fa-chevron-right"></i>',
        'type'      => 'list',
    ) );

    if ( $links ) :

    ?>
    <nav class="navigation paging-navigation" role="navigation">
        <?php echo $links; ?>
    </nav><!-- .navigation -->
    <?php
    endif;
}
endif;
?>

我在博客文章中使用了该功能,并且 home.php 页面出现了分页。 但分页没有出现在我的自定义帖子类型页面上。 我创建了名为“ervaringen”的自定义帖子类型。

在我的 page-ervaringen.php 中:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$partnersLoop = new WP_Query( array( 'post_type' => 'ervaringen', 'orderby' => 'post_id', 'posts_per_page' => 10, 'paged' => $paged ) ); 

while( $partnersLoop->have_posts() ): $partnersLoop->the_post();        
?>

<div class="post press-photo">
        <div class="image">
            <?php the_post_thumbnail(); ?>
        </div><!-- image -->
        <div class="content">
            <h5><?php the_title(); ?></h5>
            <p><?php the_excerpt(); ?></p>
        </div><!-- content -->
</div><!-- .press-photo -->

<?php endwhile; ?>

<?php procare_paging_nav(); ?>
<?php wp_reset_query(); ?>

因此,如果有人可以帮助我告诉我做错了什么,我错过了自定义帖子页面上的分页。

我尝试过但对我不起作用的其他解决方案:

更改函数以将自定义查询作为参数传递,而不是使用全局查询。

<?php

if ( ! function_exists( 'procare_paging_nav' ) ) :

function procare_paging_nav($partnersLoop) {


    // Don't print empty markup if there's only one page.
    if ( $partnersLoop->max_num_pages < 2 ) {
        return;
    }

    $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) 

    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( '?', $pagenum_link );

    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }

    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

    $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

    // Set up paginated links.
    $links = paginate_links( array(
        'base'     => $pagenum_link,
        'format'   => $format,
        'total'    => $partnersLoop->max_num_pages,
        'current'  => $paged,
        'mid_size' => 2,
        'add_args' => array_map( 'urlencode', $query_args ),
        'prev_text' => '<i class="fa fa-chevron-left"></i>',
        'next_text' => '<i class="fa fa-chevron-right"></i>',
        'type'      => 'list',
    ) );

    if ( $links ) :

    ?>
    <nav class="navigation paging-navigation" role="navigation">
        <?php echo $links; ?>
    </nav><!-- .navigation -->
    <?php
    endif;
}
endif;
?>

在 page-ervaringen.php 中

procare_paging_nav($partnersLoop);

感谢您的宝贵时间。

php wordpress pagination
1个回答
0
投票

解决方案是将自定义查询作为参数传递,而不是使用全局查询。

我之前也尝试过这个解决方案,但没有得到结果,因为我没有用自定义查询替换链接部分中的全局查询。

在@lucas的帮助下解决了问题

谢谢。

这是解决方案。如果它可以帮助将来的某人。

在 page-ervaringen.php 中

procare_paging_nav($partnersLoop);

自定义分页功能:

<?php
if ( ! function_exists( 'procare_paging_nav' ) ) :
/**
 * Display navigation to next/previous set of posts when applicable.
 *
 * @return void
 */
function procare_paging_nav($partnersLoop) {


    // Don't print empty markup if there's only one page.
    if ( $partnersLoop->max_num_pages < 2 ) {
        return;
    }

    $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;


    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( '?', $pagenum_link );

    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }

    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

    $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

    // Set up paginated links.
    $links = paginate_links( array(
        'base'     => $pagenum_link,
        'format'   => $format,
        'total'    => $partnersLoop->max_num_pages,
        'current'  => $paged,
        'mid_size' => 2,
        'add_args' => array_map( 'urlencode', $query_args ),
        'prev_text' => '<i class="fa fa-chevron-left"></i>',
        'next_text' => '<i class="fa fa-chevron-right"></i>',
        'type'      => 'list',
    ) );

    if ( $links ) :

    ?>
    <nav class="navigation paging-navigation" role="navigation">
        <?php echo $links; ?>
    </nav><!-- .navigation -->
    <?php
    endif;
}
endif;
?>
© www.soinside.com 2019 - 2024. All rights reserved.