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