jeudi 14 janvier 2021

WordPress 'circular' pagination within category for custom post type

I'm trying to build a post slider in WordPress, showing custom posts from a specific category, and adding pagination on the posts. I want this pagination to be circular, so that the last post links to the first post and vice versa.

It's almost working perfectly, but for some reason the most recent post 'previous' link is linking to itself rather than the previous post (the 'next' link is correctly going to the oldest post in that category) and I'm stumped as to why.

If I just use

<?php next_post_link('%link', '%title', TRUE); ?>
<?php previous_post_link('%link', '%title', TRUE); ?>

it works correctly but without being circular. But if I add in the If statements to show the first/last links if there isn't a next/prev item, the most recent post stops thinking it's got a previous post to link to even though it has.

This is my full code:

<?php $args = array(
            'post_type' => 'pub',
            'post_status' => 'publish',
            'posts_per_page' => 99,
            'category__in' => 17,
            'order' => 'DESC',
        );
        $arr_posts = new WP_Query( $args ); 
            $post = $posts[0]; $c=0;  ?>
                    <?php 
                 if ( $arr_posts->have_posts() ) : 
        ?>
<section class="above-fold-splash">
<div class="mySlider">
    <?php
while ( $arr_posts->have_posts() ) : 
    $arr_posts->the_post();
    $image = get_field('photo');
    $image_size_big = 'full';
$image_size_small = 'large-square';
$image_array_big = wp_get_attachment_image_src($image, $image_size_big);
$image_array_small = wp_get_attachment_image_src($image, $image_size_small);
$image_url = $image_array_big[0];
$image_url_small = $image_array_small[0];
    $logo = get_field('logo');
    if( $logo ):
            
        // Image variables.
        $logourl = $logo['url'];
        $logotitle = $logo['title'];
        $logoalt = $logo['alt'];

    endif;
    $website = get_field('website_address');
    ?>
    <div class="slide">
<div class="background-image desktop" style="background-image: url(<?php echo $image_url; ?>);"></div>
<div class="background-image mobile" style="background-image: url(<?php echo $image_url_small; ?>);"></div>
<div class="overlay"></div>

<div class="row">
    <div class="col full">
        <div class="image" data-aos="fade" data-aos-offset="0" data-aos-duration="2000" data-aos-delay="1000"><?php echo wp_get_attachment_image( $logo ); ?></div>
        <div data-aos="fade-up" data-aos-offset="0" data-aos-duration="2000" data-aos-delay="1000" class="buttons">
        <a class="button icon-none left" href="<?php echo $website; ?>"><span class="icon left"><span class="nothover"></span><span class="hover"></span></span><span class="text">Visit Website</span><span class="icon right"><span class="nothover"></span><span class="hover"></span></span></a>
        <a class="button icon-none right" href="/our-pubs"><span class="icon left"><span class="nothover"></span><span class="hover"></span></span><span class="text">See All Pubs</span><span class="icon right"><span class="nothover"></span><span class="hover"></span></span></a>
        </div>
    </div>
</div>
<div class="slider-pagination">
    <?php
$next_post = get_next_post(true);
if ( ! empty( $next_post ) ): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
    <?php echo apply_filters( 'the_title', $next_post->post_title ); ?>
</a>
<?php else: 
$args = array( 
    'post_type' => 'pub',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'category__in' => 17,
    'order' => 'ASC',
 );
        $last = new WP_Query($args); $last->the_post();
        echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
        wp_reset_postdata();
 endif; 
$prev_post = get_previous_post(true);
if ( ! empty( $prev_post ) ): ?>
<a href="<?php echo get_permalink( $prev_post->ID ); ?>">
    <?php echo apply_filters( 'the_title', $prev_post->post_title ); ?>
</a>
<?php else:    
$args = array( 
    'post_type' => 'pub',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'category__in' => 17,
    'order' => 'DESC',
 );
$first = new WP_Query($args); $first->the_post();
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
wp_reset_postdata();
 endif; ?>
</div>
</div>
<?php endwhile; else : endif;  ?>
</div>
</section>
<?php wp_reset_postdata(); ?>

Aucun commentaire:

Enregistrer un commentaire