0

I use this code to show previous and next posts (in same category) on post pages (single.php).

<?php
$next_post = get_next_post(TRUE); // TRUE = in same category
if (!empty( $next_post )): ?>
<div><a href="<?php echo get_permalink( $next_post->ID ); ?>" title="<?php echo $next_post->post_title; ?>" rel="next"><?php echo $next_post->post_title; ?></a></div>
<?php endif; ?>

<?php
$prev_post = get_previous_post(TRUE); // TRUE = in same category
if (!empty( $prev_post )): ?>
<div><a href="<?php echo get_permalink( $prev_post->ID ); ?>" title="<?php echo $prev_post->post_title; ?>" rel="prev"><?php echo $prev_post->post_title; ?></a></div>
<?php endif; ?>

It works fine, and I wish to display same thing on attachment page (image.php): previous/next post of parent post of current image.

  • Your question is a bit scrambled. As I understand it, you click on an image, you go to image.php. Now, you want to have a next/previous link going to the image of the next/previous post, but in the same category. If this is not correct, correct me then. If it is what you want, you will need to write a custom funtion for this – Pieter Goosen Dec 28 '14 at 19:13
  • I wish to have same functionality on attachment page (image.php) as on post page (single.php). On attachment page I wish to have link to previous/next post. Not to image of previous/next post, but to post itself, just like on normal post page. If you have any question do not hesitate to ask. – Advanced SEO Dec 29 '14 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.