1

I am using <!--nextpage--> to break my posts on multiple pages. This is working fine. But I want to further change the page numbering.

If you visit this post you will note the page breaks. I want to change the first page to "Question" while the second page to "Answer"

I went through the codex on this page any tried following code in my theme functions.php but it broke my site.

<?php wp_link_pages('before=To read this story, &after=. &next_or_number=next
&previouspagelink=you can go back to the previous page
&nextpagelink= or you can read on to the next page'); ?>

Could you please help to get the right code. I do not want to keep more than two pages, if this helps. And if someone can help me to get the shortcode for it will be cherry on top of ice cream :)

  • It's not clear what you mean with "shortcode for it". Something like this? – cjbj Aug 20 '16 at 16:21
  • So this is an XY question where Y is the question above, and the original question X is "I need to make a question post with an answer how do I structure the data and template?", you should have asked about your problem rather than the attempted solution – Tom J Nowell Aug 20 '16 at 16:25
2

Put this function in you functions.php -

function the_dramatist_wp_link_pages( $args = '' ) {
    global $page, $numpages, $multipage, $more;

    $defaults = array(
        'before'           => '<p>' . __( 'Pages:' ),
        'after'            => '</p>',
        'link_before'      => '',
        'link_after'       => '',
        'next_or_number'   => 'number',
        'separator'        => ' ',
        'nextpagelink'     => __( 'Next page' ),
        'previouspagelink' => __( 'Previous page' ),
        'pagelink'         => '%',
        'echo'             => 1
    );

    $params = wp_parse_args( $args, $defaults );

    /**
     * Filters the arguments used in retrieving page links for paginated posts.
     *
     * @since 3.0.0
     *
     * @param array $params An array of arguments for page links for paginated posts.
     */
    $r = apply_filters( 'wp_link_pages_args', $params );

    $output = '';
    if ( $multipage ) {
        if ( 'number' == $r['next_or_number'] ) {
            $output .= $r['before'];
            for ( $i = 1; $i <= $numpages; $i++ ) {
                if ( $i % 2 == 0) {
                    $link = $r['link_before'] . str_replace( '%', 'Answer', $r['pagelink'] ) . $r['link_after'];
                } else {
                    $link = $r['link_before'] . str_replace( '%', 'Question', $r['pagelink'] ) . $r['link_after'];
                }
                if ( $i != $page || ! $more && 1 == $page ) {
                    $link = _wp_link_page( $i ) . $link . '</a>';
                }
                /**
                 * Filters the HTML output of individual page number links.
                 *
                 * @since 3.6.0
                 *
                 * @param string $link The page number HTML output.
                 * @param int    $i    Page number for paginated posts' page links.
                 */
                $link = apply_filters( 'wp_link_pages_link', $link, $i );

                // Use the custom links separator beginning with the second link.
                $output .= ( 1 === $i ) ? ' ' : $r['separator'];
                $output .= $link;
            }
            $output .= $r['after'];
        } elseif ( $more ) {
            $output .= $r['before'];
            $prev = $page - 1;
            if ( $prev > 0 ) {
                $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>';

                /** This filter is documented in wp-includes/post-template.php */
                $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
            }
            $next = $page + 1;
            if ( $next <= $numpages ) {
                if ( $prev ) {
                    $output .= $r['separator'];
                }
                $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>';

                /** This filter is documented in wp-includes/post-template.php */
                $output .= apply_filters( 'wp_link_pages_link', $link, $next );
            }
            $output .= $r['after'];
        }
    }

    /**
     * Filters the HTML output of page links for paginated posts.
     *
     * @since 3.6.0
     *
     * @param string $output HTML output of paginated posts' page links.
     * @param array  $args   An array of arguments.
     */
    $html = apply_filters( 'wp_link_pages', $output, $args );

    if ( $r['echo'] ) {
        echo $html;
    }
    return $html;
}

And then go to your theme directory. Search and replace the wp_link_pages function names with the_dramatist_wp_link_pages. And it'll work as you wanted. Usually the wp_link_pages exists in content-{template-name}.php. So if you have any of those files in your theme please look at theme first. Here I've added a screenshot-enter image description here

And after that you do your styling. Have fun.

The function works on odd-even basis. It considers the odd part as 'Question' and the even part as 'Answer'

  • Hi @the_dramatist this is not working correctly, please check the link amexing.in/index.php/2016/08/17/hello-world i cant see both Question and Answer – Avinash Patel Aug 21 '16 at 3:45
  • @AvinashPatel, try the edited code. I think it'll do your job. :) – CodeMascot Aug 21 '16 at 6:49
  • @AvinashPatel, if this answer helped you, then would you please accept the answer ? – CodeMascot Aug 23 '16 at 11:45
1

The example in the codex has faulty quotation marks. The developer area has more detail on how to use wp_link_pages. Example usage:

<?php
$args = array(
    'before'           => '<p>' . __( 'To read this story' ),
    'after'            => '</p>',
    'link_before'      => '',
    'link_after'       => '',
    'next_or_number'   => 'next',
    'separator'        => ' ',
    'nextpagelink'     => __( 'you can go back to the previous page' ),
    'previouspagelink' => __( ' or you can read on to the next page' ),
    'pagelink'         => '%',
    'echo'             => 1
    );
wp_link_pages ($args);
?>
  • I tried this... Some how this thing has not worked. :( I don't know why. May be it was not hooked to anything.Then I tried my answer and it worked. – CodeMascot Aug 20 '16 at 16:22
  • @the_dramatist You may have a plugin that is also messing with pagination. – cjbj Aug 20 '16 at 16:25
  • Nope... I just tried your edited code. And there are two plugins installed is WooCommerce and WordPress Importer. Your code is not working. Can you please check on your system. @cjbj – CodeMascot Aug 20 '16 at 16:27
  • I've been using this code for many years without any problems. Where in your template have you tried inserting it? – cjbj Aug 20 '16 at 16:40
  • No, not in template. At functions.php.... – CodeMascot Aug 20 '16 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.