Skip Navigation

[Resolved] How to show next / previous child post?

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

Problem: I have a parent post type "Parent" with child post type "Child". Each child belongs to a single parent, and parents can have multiple children. On a single child page, I want to link to the next and previous children (sibling posts in order by post date) of the same parent using "NEXT" and "PREV" links. How can I accomplish this?

Solution: Use some custom shortcodes to create your Next and Previous buttons.

// replace "parent" and "child" with your post type slugs
add_shortcode('wpv-child-prev', 'wpv_child_prev');
add_shortcode('wpv-child-next', 'wpv_child_next');
function wpv_child_prev() {
    return wpv_child(-1, 'PREV');
}
function wpv_child_next() { 
    return wpv_child(1, 'NEXT'); 
}
function wpv_child($step, $label) {
    global $post;
    $parent = get_post_meta($post->ID, '_wpcf_belongs_parent_id', true);
    $children = get_posts(array('post_type' => 'child', 'meta_key' => '_wpcf_belongs_parent_id', 'meta_value' => $parent, 'numberposts' => -1));
    foreach ($children as $i => $child) {
        if ($child->ID == $post->ID) break;
    }
    $i += $step;
    if (isset($children[$i])) return '<a href="' . get_permalink($children[$i]) . '">' . $label . '</a>';
}

Relevant Documentation: https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

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.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 4 years, 7 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#508276

Hi.

I have a parent post type called SHOWS. I have a child post type called EPISODES.

Each show can have dozens of episodes.

Can you tell me how I can create a link to show the NEXT (or PREVIOUS) episode relative to the current one being viewed. The episodes (child posts) are ordered based on post-date.

Example. If I am viewing a specific episode (eg. episode #12). I want to have a link to show the NEXT (i.e. #13) or PREVIOUS (i.e. #11) episode.

Can you help?

#508649

Hi, it's possible to do this with a custom shortcode. Assuming your post type slugs are "shows" and "episodes":

add_shortcode('wpv-child-prev', 'wpv_child_prev');
add_shortcode('wpv-child-next', 'wpv_child_next');
function wpv_child_prev() {
    return wpv_child(-1, 'PREV');
}
function wpv_child_next() { 
    return wpv_child(1, 'NEXT'); 
}
function wpv_child($step, $label) {
    global $post;
    $show = get_post_meta($post->ID, '_wpcf_belongs_shows_id', true);
    $episodes = get_posts(array('post_type' => 'episodes', 'meta_key' => '_wpcf_belongs_shows_id', 'meta_value' => $show, 'numberposts' => -1));
    foreach ($episodes as $i => $episode) {
        if ($episode->ID == $post->ID) break;
    }
    $i += $step;
    if (isset($episodes[$i])) return '<a href="' . get_permalink($episodes[$i]) . '">' . $label . '</a>';
}

Edit - fixed typos!

#508989

Hello Christian Cox

Thanks for the help.

I think you forgot to change the variable name in the foreach loop

foreach ($works as $i => $work) {
        if ($work->ID == $post->ID) break;
    }
    $i += $step;
    if (isset($works[$i])) return '<a href="' . get_permalink($works[$i]) . '">' . $label . '</a>';
    }

replacing $works with $episode .
I managed to figure it out and now it is working fine. So i have marked this as resolved.

#508992

Ah yes, copy and paste error on my part. Glad you got this sorted out.