admin管理员组文章数量:1332389
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>
|
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>
|
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' => __('«'),
'next_text' => __('»'),
'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>
|
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>
|
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' => __('«'),
'next_text' => __('»'),
'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 badges2 Answers
Reset to default 3Since 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.
本文标签: Adding pagination to a custom archive template
版权声明:本文标题:Adding pagination to a custom archive template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742291299a2447846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论