0

I am using the below code to display thumbnail with next/prev posts links on single.php page. It is working fine when next and prev post are from same category but not showing the next or prev post link if that one is from another category.

<!-- Pagination -->
<div id="post_pagination">
    <?php $nextPost = get_next_post(true);
        if($nextPost) {
            $args = array(
                'posts_per_page' => 1,
                'in_same_cat' => false,
                'include' => $nextPost->ID
            );
            $nextPost = get_posts($args);
            foreach ($nextPost as $post) {
                setup_postdata($post);
    ?>
        <a href="<?php the_permalink(); ?>"><div class="post_next_post">
            <?php the_post_thumbnail('thumbnail'); ?>
            <span class="next">Next Story &raquo;</span>
            <h4><?php the_title(); ?></h4>
        </div></a>
    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
    ?>
    <?php $prevPost = get_previous_post(true);
        if($prevPost) {
            $args = array(
                'posts_per_page' => 1,
                'in_same_cat ' => false,
                'include' => $prevPost->ID
            );
            $prevPost = get_posts($args);
            foreach ($prevPost as $post) {
                setup_postdata($post);
    ?>
        <a href="<?php the_permalink(); ?>"><div class="post_prev_post">
            <?php the_post_thumbnail('thumbnail'); ?>
            <span class="prev">&laquo; Previous Story</span>
            <h4><?php the_title(); ?></h4>
        </div></a>
    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
    ?>
</div>
<!-- Pagination -->

So how to make it working as normal next/prev post links works...???

2
  • Link to the source of the code please. Commented Nov 24, 2014 at 5:12
  • @BradDalton Problem is solved and shared below... Commented Nov 25, 2014 at 7:30

1 Answer 1

0

After trying to ask help from others, I got my solution and here I am sharing that below. First there is no use of 'in_same_cat' => false, in upper code and second get_next_post(true);, get_previous_post(true); is used to show next/prev post from current post categories but if you want to show all posts in next/prev then you have to use only get_next_post();, get_previous_post(); instead of upper one. Finally the working code is shared below.

<!-- Pagination -->
<div id="post_pagination">
    <?php $nextPost = get_next_post();
        if($nextPost) {
            $args = array(
                'posts_per_page' => 1,
                'include' => $nextPost->ID
            );
            $nextPost = get_posts($args);
            foreach ($nextPost as $post) {
                setup_postdata($post);
    ?>
        <a href="<?php the_permalink(); ?>"><div class="post_next_post">
            <?php the_post_thumbnail('thumbnail'); ?>
            <span class="next">Next Story &raquo;</span>
            <h4><?php the_title(); ?></h4>
        </div></a>
    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
    ?>
    <?php $prevPost = get_previous_post();
        if($prevPost) {
            $args = array(
                'posts_per_page' => 1,
                'include' => $prevPost->ID
            );
            $prevPost = get_posts($args);
            foreach ($prevPost as $post) {
                setup_postdata($post);
    ?>
        <a href="<?php the_permalink(); ?>"><div class="post_prev_post">
            <?php the_post_thumbnail('thumbnail'); ?>
            <span class="prev">&laquo; Previous Story</span>
            <h4><?php the_title(); ?></h4>
        </div></a>
    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
    ?>
</div>
<!-- Pagination -->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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