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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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 3 replies, has 2 voices.
Last updated by Christian Cox 7 years ago.
Assisted by: Christian Cox.