I have created a post template that looks for other posts with the same tags. I want to paginate this list of posts. The pagination links look correct but when I click on them the pagination is lost.
For example the page is mywebsite/category/postname and the second page link points to /mywebsite/category/postname/page/2 but when I click on it the URL in the browser address bar goes back to mywebsite/category/postname.
Using similar code on a page template seems to work so not sure why it is not working on a post
The code I am using in the template is as follows:
<?php
get_header(); ?>
<div class="container container--has-padding">
<?php if ( has_post_thumbnail() ) { ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class="single-post-hero" style="background-image:url(<?php echo $url ?>);">
</div>
<div class="hero-caption"><?php the_field('hero_caption'); ?></div>
<?php } ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="single-post-meta">
<?php the_category(', '); ?>
</div>
<h1 class="single-post-title"><?php the_title(); ?></h1>
<div class="single-post-share-top">
<?php include '../includes/share.php'; ?>
</div>
<div class="single-post-content">
<?php the_content(); ?>
</div>
<div class="posts-archive container section">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo 'Currently Browsing Page ', $paged;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>10, // Number of related posts to display.
'caller_get_posts'=>1
);
global $loop;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
?>
<h2 class="text-center">Posts about this manufacturer</h2>
<ul class="grid">
<?php
while ( $loop->have_posts() ) : $loop->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<li class="grid__column grid__column--third">
<a class="thumbnail" href="<?php the_permalink(); ?>" style="background-image:url(<?php echo $url ?>);">
<?php the_post_thumbnail(); ?>
</a>
<h3 class="grid-posts-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<a class="read-more" href="<?php the_permalink(); ?>">Read more</a>
</li>
<?php
endwhile;
?>
</ul>
</div>
<div class="pager">
<ul>
<?php
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $total_pages,
'current' => $paged,
'show_all' => false,
'end_size' => 3,
'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('‹'),
'next_text' => __('›'),
'type' => 'list',
);
echo paginate_links($args);
}
?>
</ul>
<?php } ?>
</div>
<?php
wp_reset_postdata();
}
?>
<?php if( get_field('show_standard_advert') ): ?>
<div class="advert">
<?php the_field('news_category_advert_shortcode','options'); ?>
</div>
<?php endif; ?>
<div class="advert">
<?php the_field('post_specific_advert_shortcode'); ?>
</div>
<?php endwhile; ?>
</div>
I have created a post template that looks for other posts with the same tags. I want to paginate this list of posts. The pagination links look correct but when I click on them the pagination is lost.
For example the page is mywebsite/category/postname and the second page link points to /mywebsite/category/postname/page/2 but when I click on it the URL in the browser address bar goes back to mywebsite/category/postname.
Using similar code on a page template seems to work so not sure why it is not working on a post
The code I am using in the template is as follows:
<?php
get_header(); ?>
<div class="container container--has-padding">
<?php if ( has_post_thumbnail() ) { ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<div class="single-post-hero" style="background-image:url(<?php echo $url ?>);">
</div>
<div class="hero-caption"><?php the_field('hero_caption'); ?></div>
<?php } ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="single-post-meta">
<?php the_category(', '); ?>
</div>
<h1 class="single-post-title"><?php the_title(); ?></h1>
<div class="single-post-share-top">
<?php include '../includes/share.php'; ?>
</div>
<div class="single-post-content">
<?php the_content(); ?>
</div>
<div class="posts-archive container section">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo 'Currently Browsing Page ', $paged;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>10, // Number of related posts to display.
'caller_get_posts'=>1
);
global $loop;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
?>
<h2 class="text-center">Posts about this manufacturer</h2>
<ul class="grid">
<?php
while ( $loop->have_posts() ) : $loop->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<li class="grid__column grid__column--third">
<a class="thumbnail" href="<?php the_permalink(); ?>" style="background-image:url(<?php echo $url ?>);">
<?php the_post_thumbnail(); ?>
</a>
<h3 class="grid-posts-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<a class="read-more" href="<?php the_permalink(); ?>">Read more</a>
</li>
<?php
endwhile;
?>
</ul>
</div>
<div class="pager">
<ul>
<?php
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $total_pages,
'current' => $paged,
'show_all' => false,
'end_size' => 3,
'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('‹'),
'next_text' => __('›'),
'type' => 'list',
);
echo paginate_links($args);
}
?>
</ul>
<?php } ?>
</div>
<?php
wp_reset_postdata();
}
?>
<?php if( get_field('show_standard_advert') ): ?>
<div class="advert">
<?php the_field('news_category_advert_shortcode','options'); ?>
</div>
<?php endif; ?>
<div class="advert">
<?php the_field('post_specific_advert_shortcode'); ?>
</div>
<?php endwhile; ?>
</div>
Share
Improve this question
asked Jun 4, 2020 at 16:35
SkytribeSkytribe
1
1 Answer
Reset to default 0Have you tried adding the paged
parameter to your args for WP_Query
?
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>10, // Number of related posts to display.
'caller_get_posts'=>1,
'paged' => $paged
);