2

I've been successful in using the following code to display thumbnails for next & previous links for normal posts but this does not work at all for custom post types.

<div id="cooler-nav" class="navigation">
                <?php $prevPost = get_previous_post(true);
                if($prevPost) {?>
                <div class="nav-box previous">
                    <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'tiny_thumb' );?>
                    <?php previous_post_link('%link',"$prevthumbnail  <p>%title</p>", TRUE); ?>
                </div>

                <?php } $nextPost = get_next_post(true);
                if($nextPost) { ?>
                <div class="nav-box next">
                    <?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'tiny_thumb' ); } ?>
                    <?php next_post_link('%link',"$nextthumbnail  <p>%title</p>", TRUE); ?>
                </div>
                <?php  ?>
            </div><!--#cooler-nav div -->

I'm guessing it must be a special query to look for my custom post type "portfolio"

Any help would be greatly appreciated

EDIT
<nav class="clearer">

                <?php if( $prev_post = get_previous_post() ): ?>
                    <?php echo $prev_post->ID ?>
                <div class="nav-box previous">
                    <?php $prevthumbnail = get_the_post_thumbnail($prev_post->ID, 'tiny_thumb' );?>
                    <?php previous_post_link('%link',"$prevthumbnail  <p>%title</p>", TRUE); ?>
                </div>
                <?php endif; ?>

                <?php if( $next_post = get_next_post() ): ?>
                    <?php echo $next_post->ID ?>
                    <div class="nav-box next">
                        <?php $nextthumbnail = get_the_post_thumbnail($next_post->ID, 'tiny_thumb' );  ?>
                        <?php next_post_link('%link',"$nextthumbnail  <p>%title</p>", TRUE); ?>
                    </div>
                <?php endif; ?>

            </nav>
Other loop not working (I am getting thumbnails & links but no exclusion of current post or randomizing of order)
<?php   
                    $t_args = array (
                       'tax_query' => array (
                          array (
                            'orderby' => 'rand',
                            'post_type' => 'portfolio',
                            'taxonomy' => 'service',
                            'field' => 'slug',
                            'terms' => $service_type,
                            'post__not_in' => array($post->ID)

                          )
                       )
                    );

                    $query = new WP_Query( $t_args );

                    if( $query->have_posts() ) {
                       while ( $query->have_posts() ) : $query->the_post();

                          // output your stuff
                          echo '<div class="more_from_thumb"><a href="';
                          the_permalink();
                          echo '">';
                          the_post_thumbnail();
                          echo '</a></div>';
                       endwhile;
                       wp_reset_query();
                    } ?>

1 Answer 1

6

The first argument that get_previous_post and get_next_post is $in_same_cat. WordPress is looking for post of the same type in the current posts category. If your custom post type doesn't support the category taxonomy, both functions are likely to return nothing for the previous and next post.

Try calling both functions without arguments for your custom post type.

<?php if( $prev_post = get_previous_post() ): ?>
    <div class="nav-box previous">
        <?php $prevthumbnail = get_the_post_thumbnail($prev_post->ID, 'tiny_thumb' );?>
        <?php previous_post_link('%link',"$prevthumbnail  <p>%title</p>", TRUE); ?>
    </div>
<?php endif; ?>

<?php if( $next_post = get_next_post() ): ?>
    <div class="nav-box next">
        <?php $nextthumbnail = get_the_post_thumbnail($next_post->ID, 'tiny_thumb' ); } ?>
        <?php next_post_link('%link',"$nextthumbnail  <p>%title</p>", TRUE); ?>
    </div>
<?php endif; ?>
4
  • Was just about to say exactly this. The problem lies in the $in_same_cat. See this trac report Commented Feb 14, 2012 at 0:55
  • Thank you both for replying but I've had a look and cannot make sense of how to get the suggested code working. Not sure what I should & shouldn't place in the "do stuff here". If you could give me a little more detail this would be awesome. Thanks in advance
    – Meko6
    Commented Feb 14, 2012 at 6:34
  • Check out the answer now, it includes code in place of do stuff here Commented Feb 14, 2012 at 14:37
  • Thanks for doing that Christopher but unfortunately nothing is displaying. I added an echo of $prev_post->ID & next post and they are displaying the correct IDs. I also am experiencing problems with another loop (I've edited my original question to include this bit of code) the problem is that I cannot exclude the existing post OR get a random orderby.
    – Meko6
    Commented Feb 17, 2012 at 6:35

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.