get_previous_post( true ) not working with custom posts in WordPress

I am using a daughter theme of the Cherry framework. In this theme you have a custom post called portfolio. I have created posts in 2 different categories and i want the pagination to show me only the posts related to the same category as the post shown. To realize this i used the following code:

<!--BEGIN .pager .single-pager -->
            <ul class="<?php echo $left_block; ?> pager single-pager">
            <?php if (get_previous_post(true)) : ?>
                <li class="previous"><?php previous_post_link('%link', theme_locals("prev_post"), true) ?></li>
                <?php endif; ?>
                <div>
                <?php if (get_next_post(true)) : ?>
                <li class="next"><?php next_post_link('%link', theme_locals("next_post"), true) ?></li>
            <?php endif; ?>
            <!--END .pager .single-pager -->
            </ul>

But when doing this, the pagination on the page disappears. Could anyone help me out please?

Related posts

1 comment

  1. Try using this code in regards to your question:

       $prev_post = get_adjacent_post( true, '', true, 'homes-type' );;
       $next_url = ''; $next_title = '';  $prev_title = ''; $prev_url = '';
       if ( is_a( $prev_post, 'WP_Post' ) ) {
           $prev_title = strip_tags( str_replace( '"', '', $prev_post->post_title ));
       $prev_title = !empty( $prev_title ) ? $prev_title : '';
       $prev_url   = get_permalink( $prev_post->ID ) ;
       $prev_url   = !empty( $prev_url ) ? $prev_url : '';
       }
       $next_post = get_adjacent_post( true, '', false, 'homes-type' );
       if ( is_a( $next_post, 'WP_Post' ) ) {
       $next_title = strip_tags( str_replace('"', '', $next_post->post_title) );
       $next_title = !empty( $next_title ) ? $next_title : '';
       $next_url   =  get_permalink( $next_post->ID );
       $next_url   = !empty( $next_url ) ? $next_url : '';
       }
    

Comments are closed.