• Resolved flizzywp

    (@flizzywp)


    Hello,
    is there a way to let the Button (or any other shortcode) link directly to the next sibling page?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Vova

    (@gn_themes)

    Hello @flizzywp,

    unfortunately, there is no such shortcode.

    Thread Starter flizzywp

    (@flizzywp)

    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?

    Plugin Author Vova

    (@gn_themes)

    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]
    
    Thread Starter flizzywp

    (@flizzywp)

    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; 
        }
    }
    
    • This reply was modified 6 years, 4 months ago by flizzywp.
    • This reply was modified 6 years, 4 months ago by flizzywp.
    • This reply was modified 6 years, 4 months ago by flizzywp.
    Thread Starter flizzywp

    (@flizzywp)

    When I add $link between the paranthesis, it returns the URL endpoint with “Arrays” added to it

    Thread Starter flizzywp

    (@flizzywp)

    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.

    Thread Starter flizzywp

    (@flizzywp)

    Actually no, it’s not working. It shows me an error about “header information”.

    Plugin Author Vova

    (@gn_themes)

    Try to replace echo with return in 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; 
        }
    
    Thread Starter flizzywp

    (@flizzywp)

    It works! Thank you, this is absolutely great!

    Thread Starter flizzywp

    (@flizzywp)

    This must be the most useful plugin I have used in a while

    Plugin Author Vova

    (@gn_themes)

    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 www.ads-software.com, just to help me spread the word:

    https://www.ads-software.com/support/plugin/shortcodes-ultimate/reviews/#new-post

    Thank you!

    Thread Starter flizzywp

    (@flizzywp)

    Done

    Plugin Author Vova

    (@gn_themes)

    Thank you!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Next sibling page?’ is closed to new replies.