首页 > 解决方案 > WP 我的分页功能没有显示在我的自定义模板中

问题描述

我的分页功能显示在我的索引页面中,但是当在自定义模板页面(page-news.php)中调用函数时它不显示!

函数.php

function numbering_pagination() {
    global $wp_query;
    $all_pages = $wp_query->max_num_pages;
    $current_page = max(1,get_query_var('paged'));

    if ($all_pages >1) {
        return paginate_links(array(
            'base'          => get_pagenum_link() . '%_%',
            'format'        => 'page/%#%',
            'current'       => $current_page,
            'mid_size'      => 3, 
            'end_size'      => 3,
            'prev_text'     => 'السابق',
            'next_text'     =>'التالي'
        ));
    }
}

page-news.php

<?php /* Template Name: news */ 
          get_header(); ?>
    <div id="fh5co-blog-section" class="fh5co-section-gray">
        <div class="container">
            <div>
                <div class="text-center heading-section animate-box">
                    <h3>news</h3>
                </div>
            </div>
        </div>
        <div class="container">
            <div class="row row-bottom-padded-md">
            <?php
                        $args = array(
                        'post_type' => 'post',
                        'category_name'=> 'news'
                    );

                    $posts = new WP_Query( $args );
                    while( $posts->have_posts() ):
                        $posts->the_post();
                    ?>
                <div class="col-lg-4 col-md-4 col-sm-6">
                    <div class="fh5co-blog animate-box">
                    <a href="<?php the_permalink()?>"><img src="<?php the_post_thumbnail_url('full')?>" alt="" /></a>
                        <div class="blog-text">
                            <div class="prod-title">
                                <h3><a href="<?php the_permalink()?>"><?php the_title()?></a></h3>
                                <span class="posted_by"><?php the_time('F jS, Y'); ?></span>
                                <p><?php the_content('<span class="read-more"> ... more</span>'); ?></p>
                            </div>
                        </div> 
                    </div>
                </div>
                <?php 
            endwhile;
            wp_reset_query();
        ?>


        </div>
    </div>
    <!-- ------ pagination ------ -->
    <div class="pagination-numbers text-center">
     <?php echo numbering_pagination() ?>
    </div>

<!-- ------ END News ------ -->

我该如何解决这个问题?

标签: phpwordpress

解决方案


来自 WordPress 法典:

<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
'posts_per_page' => 5,
'category_name' => 'gallery',
'paged' => $paged,
);

$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->

和:

<?php
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );

?>

注意:

'total' => $the_query->max_num_pages

另见:https ://codex.wordpress.org/Function_Reference/paginate_links


推荐阅读