get_previous_post / get_next_post within same category? (WordPress)

get_previous_post / get_next_post within same category? (WordPress)

I assume its adding in_same_cat somewhere but unsure where?

<nav id="post-nav">
    <?php $prevPost = get_previous_post(true);
        if($prevPost) {
            $args = array(
                'posts_per_page' => 1,
                'include' => $prevPost->ID
            );
            $prevPost = get_posts($args);
            foreach ($prevPost as $post) {
                setup_postdata($post);
    ?>
        <div class="post-previous">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
            <span class="page_nav_block">Previ
           
        </div>
    <?php     wp_reset_postdata();
            } //end foreach
        } // end if
         
        $nextPost = get_next_post(true);
        if($nextPost) {
            $args = array(
                'posts_per_page' => 1,
                'include' => $nextPost->ID
            );
            $nextPost = get_posts($args);
            foreach ($nextPost as $post) {
                setup_postdata($post);
    ?>
        <div class="post-next">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
         
        </div>
    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
    ?>
</nav>

Source website here


Jayesh Patel
Author
Jayesh Patel

Jayesh Patel is a Professional Web Developer & Designer and the Founder of InCreativeWeb.

As a highly Creative Web/Graphic/UI Designer - Front End / PHP / WordPress / Shopify Developer, with 14+ years of experience, he also provide complete solution from SEO to Digital Marketing. The passion he has for his work, his dedication, and ability to make quick, decisive decisions set him apart from the rest.

His first priority is to create a website with Complete SEO + Speed Up + WordPress Security Code of standards.



Explore

Related Articles

18th January, 2025

Best 5 Elementor WordPress Themes (2025)

13th January, 2025

Three Easy Steps to Web Publishing on Github Pages

10th January, 2025

How to Avoid the Top 7 Web Design Errors That Degrade Your SEO

6th January, 2025

Top WordPress Social Media Plugins for 2025