0

I have added a previous and next pagination with thumbnail for my single post template but it does not showing the correct previous and next post. Also, saw its showing the previous and next post order by post ID and tried to change the order by post_date but it does not work. Below are the code of post navigation. Can someone help me please...

function wpse73190_adjacent_post_sort( $orderby ){
    return "ORDER BY p.post_date DESC LIMIT 1";
}
add_filter( 'get_previous_post_sort', 'wpse73190_adjacent_post_sort' );
add_filter( 'get_next_post_sort', 'wpse73190_adjacent_post_sort' );

function wpsites_image_nav_links() {
    if (is_singular('post')) {
        $excluded_terms = '39';
        echo '<div class="nav_container">';
        $prev_post = get_previous_post($excluded_terms);
        $next_post = get_next_post($excluded_terms);
        if (!empty( $prev_post )): ?>
            <?php $prevthumb = get_the_post_thumbnail_url( $prev_post->ID, 'full' ); ?>
            <?php echo'<span style="background:url('.$prevthumb.')" class="single-post-nav previous-post-link">'; ?>
            <a href="<?php echo $prev_post->guid ?>">
            <?php echo "<div id='borderLeft'></div><div class='shape' style='left:0; z-index:1; position: absolute; top: -15px;'>
            <div id='curved-corner-bottomleft'></div>
            <div id='curved-corner-topleft'></div>
            </div>"; ?>   
            <?php echo "<p>".$prev_post->post_title."</p>"; ?>
            </a>
            <?php echo'</span>'; ?>
        <?php endif; ?>
        
        <?php if (!empty( $next_post )): ?>
            <?php $nextthumb = get_the_post_thumbnail_url( $next_post->ID, 'full' ); ?>
            <?php echo'<span style="background:url('.$nextthumb.')" class="single-post-nav next-post-link">'; ?>
            <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>">
            <?php echo "<div id='borderRight'></div><div class='shape' style='right:0; z-index:1; position: absolute; top: -15px;'>
            <div id='curved-corner-bottomright'></div>
            <div id='curved-corner-topright'></div>
            </div>"; ?>
            <?php echo "<p>".esc_attr( $next_post->post_title )."</p>"; ?>
            </a>
            <?php echo '</span>'; ?>
        <?php endif; ?>
        <?php echo '</div>'; ?>
    <?php  }
}
2
  • $excluded_terms should be the 2nd arg to get_previous_post etc, not the first. Also those filters should not be required.
    – vancoder
    Oct 8 at 20:24
  • Great! it's solved. Thank you so much.
    – Yajuvendra
    Oct 8 at 20:44
0

$excluded_terms should be the 2nd arg to get_previous_post etc, not the first. Also those filters should not be required.

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.