Skip Navigation

[Resolved] Previous / Next within parent/child relationship

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to include pagination on my single post pages that allows a user to navigate between posts that are children of the same parent post.

Solution:
FOR AN UPDATED SOLUTION WITH RELATIONSHIPS IN TYPES 3.0+: https://toolset.com/forums/topic/next-and-previous-sibling-posts-in-a-one-to-many-post-relationship/

Legacy Relationships: Add a pagination shortcode that will output the next and previous links based on the parent and child post types:

// Add Pagination Shortcode
    function pagination() {
    $next_post = wpv_child(1, 'exhibition', 'painting');
    $previous_post = wpv_child(-1, 'exhibition', 'painting');
    $prev_pagination= '';
    $next_pagination = '';
 
    if($previous_post ){
 
        $prev_pagination = "<div class='navigation'><div class='previousnav'> <a href='".get_permalink($previous_post->ID)."'> << ".get_the_title($previous_post->ID)."</a></div>";
    }
    if($next_post){
        $next_pagination = "<div class='nextnav'><a href='".get_permalink($next_post->ID)."'> ".get_the_title($next_post->ID).">></a></div>";
    }
 
$pagination = "<div class='currentexb'><a href='http://www.neilpinkett.co.uk/npwp/exhibition/''>GALLERY</a> >".get_the_title()."</div>";
 
 
return $prev_pagination.$pagination.$next_pagination;
 
}
add_shortcode( 'pagination', 'pagination' );
 
function wpv_child($step, $parentslug, $childslug) {
    global $post;
    $parent = get_post_meta($post->ID, '_wpcf_belongs_' . $parentslug
      . '_id', true);
    $children = get_posts(array('post_type' => $childslug, 'meta_key' => '_wpcf_belongs_' . $parentslug . '_id', 'meta_value' => $parent, 'numberposts' => -1));
    $i = 0;
    foreach ($children as $i => $child) {
        if ($child->ID == $post->ID) break;
    }
    $i += $step;
    if (isset($children[$i])) return $children[$i];
}
This support ticket is created 7 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 3 voices.

Last updated by alexd-6 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#572145

Hi Shane

You previously helped me with my prev/next navigation in this thread https://toolset.com/forums/topic/either-adding-prevnext-to-content-template-or-moving-related-posts-to-theme/

Would you be able to offer a little more assistance with this. I'll try to explain what I need:
If you look at this page it shows you all the paintings that are in the 'A Year at Cape Cornwall 2017' exhibition: hidden link
The exhibition is the parent content type and the paintings are all children of this.

I would like the Prev/Next links to be restricted to the paintings that are connected to the parent. But at the moment they take you through all the paintings, including those with different parents. Is there any way of doing this?

Many thanks, Emily

#572228

Hi, please replace the code provided earlier with the updated code:

// Add Pagination Shortcode
function pagination() {
    $next_post = wpv_child(1, 'parent-ptypes', 'child');
    $previous_post = wpv_child(-1, 'parent-ptypes', 'child');
    $prev_pagination= '';
    $next_pagination = '';

    if($previous_post ){

        $prev_pagination = "<div class='navigation'><div class='previousnav'> <a href='".get_permalink($previous_post->ID)."'> << ".get_the_title($previous_post->ID)."</a></div>";
    }
    if($next_post){
        $next_pagination = "<div class='nextnav'><a href='".get_permalink($next_post->ID)."'> ".get_the_title($next_post->ID).">></a></div>";
    }

$pagination = "<div class='currentexb'><a href='<em><u>hidden link</u></em>''>GALLERY</a> >".get_the_title()."</div>";


return $prev_pagination.$pagination.$next_pagination;

}
add_shortcode( 'pagination', 'pagination' );

function wpv_child($step, $parentslug, $childslug) {
    global $post;
    $parent = get_post_meta($post->ID, '_wpcf_belongs_' . $parentslug
      . '_id', true);
    $children = get_posts(array('post_type' => $childslug, 'meta_key' => '_wpcf_belongs_' . $parentslug . '_id', 'meta_value' => $parent, 'numberposts' => -1));
    foreach ($children as $i => $child) {
        if ($child->ID == $post->ID) break;
    }
    $i += $step;
    if (isset($children[$i])) return $children[$i];
}

Let me know the results and we can go from there.

#572492

Hi Christian

Thanks for your help with this. I've added it to a dev version of the site here: hidden link
As you can see, the prev/next links have completely disappeared.

The version of the function that I'm currently using here hidden link is:

// Add Pagination Shortcode
	function pagination() {
		$next_post = get_previous_post();
		$previous_post = get_next_post();
		$prev_pagination= '';
		$next_pagination = '';

		if($previous_post != 0 ){
			
			$prev_pagination = "<div class='previousnav'> <a href='".get_permalink($previous_post->ID)."'> < Previous</a></div>";
		}
		if($previous_post == 0 ){
			
			$prev_pagination = "<div class='previousnav'></div>";
		}
		if($next_post != 0){
			$next_pagination = "<div class='nextnav'><a href='".get_permalink($next_post->ID)."'> Next ></a></div>";
		}
		if($next_post == 0){
			$next_pagination = "<div class='nextnav'></div>";
		}

    $pagination = "<div class='currentexb'>PAINTINGS IN THIS SERIES</div>";
	

    return $prev_pagination.$pagination.$next_pagination;
 
	}
	add_shortcode( 'pagination', 'pagination' );

Many thanks, Emily

#573015

Hi, there's one bit of code that you must update based on your parent post type slug:

$next_post = wpv_child(1, 'parent-ptypes', 'child');
$previous_post = wpv_child(-1, 'parent-ptypes', 'child');

Please replace 'parent-ptypes' with the slug of your parent post type and let me know the results.

#573268

Hi Christian

I've replaced these two lines so that they say:

$next_post = wpv_child(1, 'exhibition', 'child');
$previous_post = wpv_child(-1, 'exhibition', 'child');

'exhibition' is the slug of the parent post type

Unfortunately it results in this (a mostly blank page): hidden link

Cheers, Emily

#573416

Ok at this point it probably makes more sense for me to log in to your site, create a clone, and set this up for you so we're not passing errors back and forth. If you agree, please provide login credentials in the private reply fields here. I will install the Duplicator plugin and create a clone of your site, where I can do the necessary updates. Then when they're working, I will let you know what you should add to your code.

#573799

Hi, I've updated your dev site's functions.php file to include the correct pagination shortcode:

// Add Pagination Shortcode
    function pagination() {
    $next_post = wpv_child(1, 'exhibition', 'painting');
    $previous_post = wpv_child(-1, 'exhibition', 'painting');
    $prev_pagination= '';
    $next_pagination = '';

    if($previous_post ){

        $prev_pagination = "<div class='navigation'><div class='previousnav'> <a href='".get_permalink($previous_post->ID)."'> << ".get_the_title($previous_post->ID)."</a></div>";
    }
    if($next_post){
        $next_pagination = "<div class='nextnav'><a href='".get_permalink($next_post->ID)."'> ".get_the_title($next_post->ID).">></a></div>";
    }

$pagination = "<div class='currentexb'><a href='<em><u>hidden link</u></em>''>GALLERY</a> >".get_the_title()."</div>";


return $prev_pagination.$pagination.$next_pagination;

}
add_shortcode( 'pagination', 'pagination' );

function wpv_child($step, $parentslug, $childslug) {
    global $post;
    $parent = get_post_meta($post->ID, '_wpcf_belongs_' . $parentslug
      . '_id', true);
    $children = get_posts(array('post_type' => $childslug, 'meta_key' => '_wpcf_belongs_' . $parentslug . '_id', 'meta_value' => $parent, 'numberposts' => -1));
    $i = 0;
    foreach ($children as $i => $child) {
        if ($child->ID == $post->ID) break;
    }
    $i += $step;
    if (isset($children[$i])) return $children[$i];
}

Can you take a look and confirm the links are acting appropriately?

#574022

Hi Christian

That's working perfectly. You're a star! Thanks so much for your help with this.

Cheers, Emily

#616427

øh nice - thanx!

#1677563

Hey - in my code if i in first Item - it shows in paginationprev child from other Parent. Here is my code and URL where you can see it:

hidden link

[php]// Add Pagination Shortcode
function paginationprev() {
$next_post = wpv_child(1, 'projekt', 'objekt');
// $previous_post = wpv_child(-1, 'projekt', 'objekt');
// $prev_pagination= '';
$next_pagination = '';

if($next_post ){

$next_pagination = "<div class='previousnav'> << ".get_the_title($next_post->ID)."</div>";
}

//$pagination = "zurück >";

return '<div class="objektpagination">'.$next_pagination;

}
add_shortcode( 'paginationprev', 'paginationprev' );

### Child Navigation ###
// Add Pagination Shortcode
function paginationnext() {
//$next_post = wpv_child(1, 'projekt', 'objekt');
$previous_post = wpv_child(-1, 'projekt', 'objekt');
$prev_pagination= '';
//$next_pagination = '';

if($previous_post){
$prev_pagination = "<div class='nextnav'> ".get_the_title($previous_post->ID)." >> </div>";
}

//$pagination = "zurück >";

return $prev_pagination.'</div>';

}
add_shortcode( 'paginationnext', 'paginationnext' );

function wpv_child($step, $parentslug, $childslug) {
global $post;
$parent = get_post_meta($post->ID, '_wpcf_belongs_' . $parentslug
. '_id', true);
$children = get_posts(array('post_type' => $childslug, 'meta_key' => '_wpcf_belongs_' . $parentslug . '_id', 'meta_value' => $parent, 'numberposts' => -1));
$i = 0;
foreach ($children as $i => $child) {
if ($child->ID == $post->ID) break;
}
$i += $step;
if (isset($children[$i])) return $children[$i];
}
/code>

can you help me again?

thx and best regards - alex

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/previous-next-within-parent-child-relationship/