0

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 ?>);">
&nbsp;
</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' => __('&lsaquo;'),
                'next_text' => __('&rsaquo;'),
                '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>
0

Have 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
);
| improve this answer | |
  • Thanks yes tried that but makes no difference. The pagination doesn't seem to get back to the post it is lost somehow. The pagination links created look correct but the /page/n is lost when you click on them. – Skytribe Jun 5 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.