Skip Navigation

[Resolved] Previous andnext post links, but limited to the same parent

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

Problem: I would like to add pagination between sibling posts in a one-to-many post relationship. The links should say "Next" and "Previous" and allow the User to easily paginate between sibling posts with the same parent post.

Solution: A custom shortcode is available to help create these links. See the links below for some code examples and customizations.

Relevant Documentation:
https://toolset.com/forums/topic/next-and-previous-sibling-posts-in-a-one-to-many-post-relationship/
https://toolset.com/forums/topic/previous-en-next-post-links-but-limited-to-the-same-parent/#post-2049429
https://toolset.com/forums/topic/previous-en-next-post-links-but-limited-to-the-same-parent/#post-2053171

This support ticket is created 2 years, 10 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 8 replies, has 3 voices.

Last updated by Marcel 2 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#2046157

I'm want to add links to the next and previous post on a single artworks page such as this one: hidden link.

As you can see, I got that working, using the [wpv-post-previous-link] and [wpv-post-next-link] shortcodes. However, those will link to posts from different parents as well, whilst I need them to only link to posts from the same parents.

I did find this topic: https://toolset.com/forums/topic/how-to-show-next-previous-child-post/, but I'm not sure which instances of "parent" and "child" I should replace with my posts slug. Also, that topic is 4 years old, so maybe there's native support for this by now?

#2046691

Hello, there's no native support for this in the current software, but there is an updated code snippet available for post relationships in Types 3+. You can find that updated code here with examples:
https://toolset.com/forums/topic/next-and-previous-sibling-posts-in-a-one-to-many-post-relationship/

The syntax has changed and you only need to know the relationship slug now, not the post type slugs for parent and child. Let me know if you have questions about this.

#2048329

Thanks Christian. I just need a bit more help: I want the links to say "previous" and "next" instead of the post title. I guess I need to replace the $title in this rule":

$link .= "<a href='" . $perm . "'>" . $title . "</a>";

but with what?

#2049429

Okay sure, to display "Next" or "Previous" as the link text, instead of the post title, you can replace this 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>";
  }

...with this update:

// create link to next/previous sibling
  if(isset($siblings[$i])){
    $perm = get_the_permalink( $siblings[$i] );
    $linktext = $a['step']=='1' ? "Next" : "Previous";
    $link .= "<a href='" . $perm . "'>" . $linktext . "</a>";
  }

Let me know if this does not resolve the issue for you.

#2050225

Hi Christian,

That seems to work, thanks. However, the original part of the code does code a fatal error when I try to edit the template:

Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /home/artandcommunication.nl/public_html/artcommunicators/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246 Stack trace: #0 /home/artandcommunication.nl/public_html/artcommunicators/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'parent') #1 /home/artandcommunication.nl/public_html/artcommunicators/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(0, 'artist-artwork', Array) #2 /home/artandcommunication.nl/public_html/artcommunicators/wp-content/themes/Avada-Child-Theme/functions.php(40): toolset_get_related_posts(0, 'artist-artwork', Array) #3 /home/artandcommunicati in /home/artandcommunication.nl/public_html/artcommunicators/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246
There has been a critical error on this website. Please check your site admin email inbox for instructions.

#2051085

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Marcel,

Christian is currently on a public holiday today but he will be back tomorrow to continue assisting you with this one.

Thank you for the patience and understanding.

#2052073

Okay I think I will need to take a closer look at how this is implemented on your site to be able to give you an update. Is the theme editable in wp-admin? If so, I can log in and check it out. Please provide login credentials in the private reply fields here and I'll take a look.

#2053171

Okay I edited your child theme's functions.php file and added the last three lines of code here after the $sibling_args array. This conditional structure will prevent fatal errors in the Block Editor when the parent ID is false-y because wpv-post-id has no related parent post ID for any reason:

$sibling_args = array(
    'query_by_role'=>'parent',
    'limit'=>1000,
    'role_to_return'=>'child',
    'orderby'=>'title'
  );
  // prevent fatal errors: see https://toolset.com/forums/topic/previous-en-next-post-links-but-limited-to-the-same-parent/
  if( !$parent_id )
	  return;

Now it seems to be functioning as expected in a few quick tests on the front-end. Can you confirm?

By the way, as long as you're in there, could you also check on this issue please? https://toolset.com/forums/topic/html-gets-corrupted-everytime-i-open-the-template/

Our policy is to address one issue per ticket, so I would prefer to continue discussing the sibling navigation issue in the current ticket. It looks like the other ticket was closed without a real resolution, so I will reopen that ticket momentarily and ask Jamal to follow up with you.

#2053189

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.