Adding pagination to a custom archive template

I'm trying to add pagination to my archive template. If I use the same code as on my main blog template it doesn�

I'm trying to add pagination to my archive template. If I use the same code as on my main blog template it doesn't filter results based on the tag in the URL, instead it simply shows all posts. Probably obvious to most but clearly I'm missing something.

I've managed to get the archive page showing just the relevant posts using the following code (but this contains no pagination):

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="col span_1_of_1 border_bottom">
        <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="blog_meta_information date">
            Author: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            Date: <?php the_time('jS F Y') ?>
            <?php if(has_tag()) { ?>
                <br>
                Tags: <?php the_tags( '',', ','' ); ?>
            <?php } else {} ?>
        </p>
        <p><?php the_field('introduction'); ?></p>
        <a href="<?php the_permalink(); ?>">Read more</a>
    </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

I've also got my main blog working with pagination using the following code:

<?php 
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $custom_args = array(
        'post_type' => 'post',
        'posts_per_page' => 10,
        'paged' => $paged
    );
    $custom_query = new WP_Query( $custom_args ); 
?>
<?php if( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    <div class="col span_1_of_1 border_bottom">
        <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="blog_meta_information date">
            Author: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            Date: <?php the_time('jS F Y') ?>
            <?php if(has_tag()) { ?>
                <br>
                Tags: <?php the_tags( '',', ','' ); ?>
            <?php } else {} ?>
        </p>
        <p><?php the_field('introduction'); ?></p>
        <a href="<?php the_permalink(); ?>">Read more</a>
    </div>
<?php endwhile; endif; wp_reset_postdata(); ?>
<!-- PAGINATION -->
<?php
    if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
    }
?>

The above also uses the following code from my functions.php file:

// PAGINATION
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    if (empty($pagerange)) {
        $pagerange = 2;
    }
    /**
     * This first part of our function is a fallback
     * for custom pagination inside a regular loop that
     * uses the global $paged and global $wp_query variables.
     * 
     * It's good because we can now override default pagination
     * in our theme, and use this function in default queries
     * and custom queries.
     */
    global $paged;
    if (empty($paged)) {
        $paged = 1;
    }
    if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
                $numpages = 1;
        }
    }
    /** 
     * We construct the pagination arguments to enter into our paginate_links
     * function. 
     */
    $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'       => __('&laquo;'),
        'next_text'       => __('&raquo;'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
    );
    $paginate_links = paginate_links($pagination_args);
    if ($paginate_links) {
        echo "<div class='custom-pagination'>";
            echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
            echo $paginate_links;
        echo "</div>";
    }
}

Anyone know how I can add make this pagination also work with my archive template?

Thanks in advance,

Tom

I'm trying to add pagination to my archive template. If I use the same code as on my main blog template it doesn't filter results based on the tag in the URL, instead it simply shows all posts. Probably obvious to most but clearly I'm missing something.

I've managed to get the archive page showing just the relevant posts using the following code (but this contains no pagination):

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="col span_1_of_1 border_bottom">
        <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="blog_meta_information date">
            Author: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            Date: <?php the_time('jS F Y') ?>
            <?php if(has_tag()) { ?>
                <br>
                Tags: <?php the_tags( '',', ','' ); ?>
            <?php } else {} ?>
        </p>
        <p><?php the_field('introduction'); ?></p>
        <a href="<?php the_permalink(); ?>">Read more</a>
    </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

I've also got my main blog working with pagination using the following code:

<?php 
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $custom_args = array(
        'post_type' => 'post',
        'posts_per_page' => 10,
        'paged' => $paged
    );
    $custom_query = new WP_Query( $custom_args ); 
?>
<?php if( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    <div class="col span_1_of_1 border_bottom">
        <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="blog_meta_information date">
            Author: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            Date: <?php the_time('jS F Y') ?>
            <?php if(has_tag()) { ?>
                <br>
                Tags: <?php the_tags( '',', ','' ); ?>
            <?php } else {} ?>
        </p>
        <p><?php the_field('introduction'); ?></p>
        <a href="<?php the_permalink(); ?>">Read more</a>
    </div>
<?php endwhile; endif; wp_reset_postdata(); ?>
<!-- PAGINATION -->
<?php
    if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
    }
?>

The above also uses the following code from my functions.php file:

// PAGINATION
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    if (empty($pagerange)) {
        $pagerange = 2;
    }
    /**
     * This first part of our function is a fallback
     * for custom pagination inside a regular loop that
     * uses the global $paged and global $wp_query variables.
     * 
     * It's good because we can now override default pagination
     * in our theme, and use this function in default queries
     * and custom queries.
     */
    global $paged;
    if (empty($paged)) {
        $paged = 1;
    }
    if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
                $numpages = 1;
        }
    }
    /** 
     * We construct the pagination arguments to enter into our paginate_links
     * function. 
     */
    $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'       => __('&laquo;'),
        'next_text'       => __('&raquo;'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
    );
    $paginate_links = paginate_links($pagination_args);
    if ($paginate_links) {
        echo "<div class='custom-pagination'>";
            echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
            echo $paginate_links;
        echo "</div>";
    }
}

Anyone know how I can add make this pagination also work with my archive template?

Thanks in advance,

Tom

Share Improve this question asked Sep 19, 2018 at 15:24 Tom PerkinsTom Perkins 1351 gold badge2 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Since 4.1.0, WordPress introduce the_posts_pagination to handle number paginate link. I use it all the time, and it just works. With any custom post type. You'll want to use that function after the while loop.

See: https://developer.wordpress/reference/functions/the_posts_pagination/

Just a quick update on this one: I chose to use the WP-PageNavi plugin (https://en-gb.wordpress/plugins/wp-pagenavi/) on the archives page and leave the main blog using the custom pagination which does the job perfectly.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742291299a4416238.html

相关推荐

  • Adding pagination to a custom archive template

    I'm trying to add pagination to my archive template. If I use the same code as on my main blog template it doesn�

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信