Next sibling page?
-
Hello,
is there a way to let the Button (or any other shortcode) link directly to the next sibling page?
-
Hello @flizzywp,
unfortunately, there is no such shortcode.
I don’t know much about WP development, but I have a PHP function that when called generates the correct URL.
Is there a way to call this PHP function from a button click?Wrap your function with a shortcode. Put the following code into your functions.php file in your active theme directory:
add_shortcode( 'next_sibling_page', function( $atts, $content ) { return your_function_which_returns_url(); } );Then, use created shortcode within a button:
[su_button url="{next_sibling_page}"] Next Sibling Page [/su_button]It says “missing argument” with a lot error text.
This is the exact PHP function, can you tell me how the shortcode code has to look exactly?Sorry I can’t manage to format the code. The code tag doesn’t work.
function get_sibling_link($link) { global $post; $siblings = get_pages('sort_column=menu_order&child_of='.$post->post_parent.'&parent='.$post->post_parent); foreach ($siblings as $key => $sibling){ if ($post->ID == $sibling->ID){ $current_id = $key; } } $closest = [ 'before' => get_permalink( $siblings[$current_id-1]->ID ), 'after' => get_permalink( $siblings[$current_id+1]->ID ) ]; if($siblings[$current_id-1]->ID == '' ){ $closest['before'] = get_permalink( $siblings[count($siblings)-1]->ID ); } if($siblings[$current_id+1]->ID == '' ){ $closest['after'] = get_permalink( $siblings[0]->ID ); } if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; } }When I add $link between the paranthesis, it returns the URL endpoint with “Arrays” added to it
OK I figured out that I had to pass “after” to the function. The problem now is, instead of putting the link into the button, it is displayed as text above the button.
Actually no, it’s not working. It shows me an error about “header information”.
Try to replace
echowithreturnin the end of your function.// replace this if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; } // with this if ($link == 'before' || $link == 'after') { return $closest[$link]; } else { return $closest; }It works! Thank you, this is absolutely great!
This must be the most useful plugin I have used in a while
Awesome! Happy I could help you out. Btw. If you have a moment, I would very much appreciate if you could quickly rate the plugin on WordPress.org, just to help me spread the word:
https://wordpress.org/support/plugin/shortcodes-ultimate/reviews/#new-post
Thank you!
Done
Thank you!
The topic ‘Next sibling page?’ is closed to new replies.