Support » Plugins » Hacks » Get parent post pagination links

  • Hi,

    I’m trying to get the links for the previous and next posts of the parent post when viewing an image attachment. This is the code I have so far:

    		$parentID = $parent->ID;
    		$parentPostType = $parent->post_type;
    		$args = array('post_type' => $parentPostType);
    		$loop = new WP_Query($args);?>
    		<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    			<?php previous_post_link(); ?>
    			<?php next_post_link(); ?>
    	<?php endwhile; ?>

    Does anyone have any idea where I’m going wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I don’t see you actually using that $parentID variable in your new $loop query, just the post type.

    Yeah, I removed that from the loop. Even with the $parentID in the loop as follows:

    'p'=> $parentID

    It gives me two previous buttons, the post attached to the image and the previous post, and the next button the post attached to the image.

    So, for example, I have two posts, test and test2 and when viewing the image attached to test, it displays test2 and test as the previous buttons and test2 as the next button.

    What I’m trying to do is to just display the test2 next link.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get parent post pagination links’ is closed to new replies.