Skip Navigation

[Resolved] Show content based on whether there is a post before or after the current post

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

Problem: I would like to show some content based on whether or not there is an adjacent post to the current post.

Solution: Here is the custom shortcode for reference:

function ts_get_adjacent_post_url_func($atts) {
  global $post;
  $previous = ($atts['previous'] == "1");
  $adjacent = get_adjacent_post( false, '', $previous);
  return get_the_permalink($adjacent);
}
add_shortcode("ts_get_adjacent_post_url", "ts_get_adjacent_post_url_func");

If no adjacent post exists, the URL for returned by the custom shortcode will be equal to the URL of the current page. Here's how you can use conditional HTML to test the current page's URL against the previous page URL:

[wpv-conditional if="( '[ts_get_adjacent_post_url previous='1']' ne '[wpv-post-url id='$current_page']' )"]Only show if there is a previous post in list[/wpv-conditional]
[wpv-conditional if="( '[ts_get_adjacent_post_url previous='1']' eq '[wpv-post-url id='$current_page']' )"]Only show if there is not a previous post in list[/wpv-conditional]

Relevant Documentation:
https://toolset.com/forums/topic/display-wpv-post-next-link-and-wpv-post-previous-link-as-raw-html-link/#post-619757

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

Last updated by Kate 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#710255
Capture.JPG

I'm having trouble conditionally displaying adjacent post URLS.

Ideally i would like to: NOT SHOW content, if there is not a 'previous' Post. And NOT SHOW content, if there not a 'next' post.

[wpv-conditional if="( '[ts_get_adjacent_post_url previous='1']' )"]Only show if there is a previous post in list[/wpv-conditional]
[wpv-conditional if="( '[ts_get_adjacent_post_url previous='0']' )"]Do not show if there is a previous post in list[/wpv-conditional]

The page I am testing on, is the first in the list of pages : hidden link

Thanks!

(refer to closed ticket: https://toolset.com/forums/topic/hiding-previous-and-next-buttons-for-first-and-last-post-archive/)

#711968

Hi, the conditionals must "test" something, like is A = B, or is A greater than B. The conditionals you have here don't test anything, because there is no comparison yet. What you want to test: Is the URL returned by the custom shortcode equal to the URL of the current page? If yes, then that indicates there is no previous or next post, depending on whether previous is 1 or 0. So here's an example I was thinking of:

[wpv-conditional if="( '[ts_get_adjacent_post_url previous='1']' ne '[wpv-post-url id='$current_page']' )"]Only show if there is a previous post in list[/wpv-conditional]
[wpv-conditional if="( '[ts_get_adjacent_post_url previous='1']' eq '[wpv-post-url id='$current_page']' )"]Only show if there is not a previous post in list[/wpv-conditional]

The 'eq' is a comparison shortcut for "equal to", and 'ne' is a shortcut for "not equal to".

#712250

Amazing, thank you Christian! This works perfectly 🙂

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