In this example, we have created a custom shortcode you can use to display pagination links like “Next: Next Sibling Post Title” and “Previous: Previous Sibling Post Title” to navigate among sibling child posts in a one-to-many post relationship. You can place this shortcode in the child post template to generate the appropriate link programmatically and display it only if necessary.
// get a link to the next or previous sibling post in child post content template // example: [tssupp-next-prev-child current="[wpv-post-id]" step="1" relslug="book-chapter"][/tssupp-next-prev-child] // @current: current post id // @step: 1 for next or -1 for previous // @relslug: slug of the post relationship function tssupp_next_prev_child($atts) { $a = shortcode_atts( array( 'current' => 0, 'step' => '1', 'relslug' => '', ), $atts ); $link = ''; // get all child posts $relationship_slug = $a['relslug']; $current_child_id = $a['current']; $parent_id = toolset_get_related_post( $current_child_id, $relationship_slug ); $sibling_args = array( 'query_by_role'=>'parent', 'limit'=>1000, 'role_to_return'=>'child', 'orderby'=>'title' ); $siblings = toolset_get_related_posts( $parent_id, $relationship_slug, $sibling_args ); // loop over child posts and get index of the current post foreach($siblings as $i=>$sibling) { if( $sibling == $current_child_id ) { break; } } // increment or decrement index for next or previous sibling $i += $a['step']; // create link to next/previous sibling if(isset($siblings[$i])){ $perm = get_the_permalink( $siblings[$i] ); $title = get_the_title( $siblings[$i] ); $link .= $a['step']=='1' ? "Next: " : "Previous: "; $link .= "<a href='" . $perm . "'>" . $title . "</a>"; } // output the link, or empty string if not set return $link; } add_shortcode( 'tssupp-next-prev-child', 'tssupp_next_prev_child' );
Use the shortcode like this to generate a “Next” link (if necessary):
[tssupp-next-prev-child step="1" relslug="book-chapter" current="[wpv-post-id]"][/tssupp-next-prev-child]
Replace book-chapter with the slug of the post relationship.
You’ll change the step value to -1 to generate a “Previous” link (if necessary):
[tssupp-next-prev-child step="-1" relslug="book-chapter" current="[wpv-post-id]"][/tssupp-next-prev-child]
Again, replace book-chapter with the slug of the post relationship.
If you want to modify the links, you should only need to edit this part of the code:
// create link to next/previous sibling if(isset($siblings[$i])){ $perm = get_the_permalink( $siblings[$i] ); $title = get_the_title( $siblings[$i] ); $link .= $a['step']=='1' ? "Next: " : "Previous: "; $link .= "<a href='" . $perm . "'>" . $title . "</a>"; }
Support Thread Example
Let us know if this snippet is not working for you:
This snippet doesn’t work