wp_link_pages shortcode for 'nextpagelink'

I want 'nextpagelink' shortcode [nextpage_shortcode] that I can place inside content.

I've seen threads where they say that you can't place anything related to the wp_link_pages inside content due to the way the content is rendered by Wordpress.

As you can see the text of 'nextpagelink' changes depending on the slide #.

I want the shortcode to reflect that.

Here is the schematic of what I am looking to do:

Both 'BEGIN' buttons link to the 2nd slide.

This is what I am currently working on:

function nextpage_shortcode( $atts ) { wp_link_pages( array(

'before' = 'div id="slideshow"',
'after' = '/div',
'next_or_number' = 'next', 
'nextpagelink' = ( get_query_var('page')  2 ) ? 'span id="next" BEGIN /span' : 'span id="next" NEXT /span' ) );

return ['nextpagelink'];
} add_shortcode( 'nextpage_short', 'nextpage_shortcode' );

Its works somewhat.

Its posts the 'nextpagelink' button above content, but not inside the content.

Not sure how to proceed.

Please help!

Topic wp-link-pages shortcode pagination Wordpress

Category Web


wp_link_pages() by default echoes the links (or the output), unless you set the echo parameter to 0 (or false). Secondly, your code is returning an array with nextpagelink as the only item/value.

But anyway, if you just want the link to the next page, the wp_link_pages() wouldn't be the one you should be using since it outputs all the page links (start, next, previous, etc.).

Instead, you can try this, which works well for me:

function nextpage_shortcode( $atts ) {
    global $page, $numpages, $multipage;
    if ( $multipage ) {
        $i = max( 1, $page );
        if ( $i < 2 ) {
            $text = '<span id="next"> BEGIN </span>';
        } elseif ( $i < $numpages ) {
            $text = '<span id="next"> NEXT </span>';
        }
        if ( ! empty( $text ) ) {
            $link = _wp_link_page( $i + 1 );
            return $link . $text . '</a>';
        }
    }
    return '';
}
add_shortcode( 'nextpage_short', 'nextpage_shortcode' );

However, you should know that I'm using _wp_link_page() which is a private function. So you might want to copy the source, rename the function (e.g. to my_link_page), and use my_link_page() in the above code.

I hope that helps, and remember that the <!--more--> tag (i.e. content teaser) is not being taken into account since you're specifically dealing with the <!--nextpage--> tag only.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.