0

I would like to work the wp_link_pages functions a bit.

In header of the post starting with second page I would like to have a link to previous page,

Continued from page 1

where 1 is the number of the previous page, if is first page will show up nothing.

In the footer of the post I would like them to show up like this:

<< Previous Page | 1 | 2 | 3 | 4 | Next Page >> 

and the current page number should not be linked.

Thanks

  • This should be fairly simple. What have you tried, exactly, and what doesn't work? Post your code please. – s_ha_dum Aug 20 '13 at 23:11
  • I tried with Wordpress Codex (codex.wordpress.org/Styling_Page-Links) but could not find the right parameters. I know its easy, but I don't know how :) – Robert Ramsey Aug 20 '13 at 23:41
1

For the header

First of all you have to know how retrieve the current page, that is done by

get_query_var('page')

Knowing it, writing what you want is easy. It can be a little hard if you want to link the previous page correctly. In this case you have to use an internal function of wordpress, _wp_link_page:

$now = get_query_var('page');
if  ( $now > 1 ) {
  $prev = $now-1;
  $anchor =  sprintf( __('Continued from page %d'), $prev );
  $prev_link = _wp_link_page( $prev ) . $anchor . '</a>';
  echo '<p>' . apply_filters( 'wp_link_pages_link', $prev_link, $prev ) . '</p>';
}

For the footer

If you see at Codex for wp_link_pages you understand that the behavior of this function is usually showing page number or showing next and previous link, not both as you want. For me, solution is create a customized version of the function, putting next code in the functions.php:

function wp_link_pages_customized() {
  global $page, $numpages, $multipage;
  if ( $multipage ) {
    $output = '';
    $prev =  ( $page < 2 ) ? false : _wp_link_page( $page-1 ) . __('&laquo; Previous page ') . '</a>';
    $next =  ( $page == $numpages ) ? false : _wp_link_page( $page+1 ) . __('Next page &raquo;') . '</a>';
    for ( $i = 1; $i <= $numpages; $i++ ) {
      if ( $i != $page ) {
        $output .= _wp_link_page( $i ) . $i . '</a> | ';
      } else {
        $output .= $i . ' | ';
      }
    }
    if ( $prev ) $output = $prev . ' | ' . $output;
    $output = ( $next ) ? $output . $next : rtrim($output, '| ');
    echo $output;
  }
}

and then, in the footer template, simply use like so:

<p><?php wp_link_pages_customized(); ?></p>
  • On line 4 for the footer code, you've missed a comma :) . How can I make to have the full "Continued from page 1" a link ? Not just the page number. Thanks. – Robert Ramsey Aug 21 '13 at 14:32
  • I've added the comma and edited the code to make all "Continued from page 1" clickable. I've not tested, but should works – gmazzap Aug 21 '13 at 14:54
  • The header code works just fine, I would like to slightly change the footer code so it will output the separators between page number as no linkable, right now the "after" "|" element is linkable. Thanks again and sorry for asking so many things, I would definitely should start learning PHP. Also the footer code makes the closest text linkable when is on first page. – Robert Ramsey Aug 21 '13 at 19:39
  • @RobertRamsey edited completely the code for the footer. Now it should work as you want. – gmazzap Aug 21 '13 at 20:26

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.