0

I'm looking to modify how Wordpress is showing the pages of my posts. What I currently have is a previous and next page link at the bottom of the post with the following code:

<?php wp_link_pages( array( 'before' => '<div class="page-link-next-prev">',
 'after' => '</div>', 'next_or_number' => 'next', 'previouspagelink' =>
 __('Previous'), 'nextpagelink'=> __('Next')  ) );?>

Which would render: Previous Next

What I want to show is: Previous 1 of 5 Next The page where I am and the total of pages in between both buttons.

Also, is there any way that when I am on page 5 of 5 the next button still appears but goes to the next article?

Thanks a lot!

2 Answers 2

5

This code should help you achieve what you want to, if you swap the code below

<?php wp_link_pages( array( 'before' => '<div class="page-link-next-prev">',
 'after' => '</div>', 'next_or_number' => 'next', 'previouspagelink' =>
 __('Previous'), 'nextpagelink'=> __('Next')  ) );?>

With this code

<?php 

global $page, $pages;

// This shows the Previous link
wp_link_pages( array( 'before' => '<div class="page-link-next-prev">', 
'after' => '', 'previouspagelink' => 'Previous', 'nextpagelink' => '', 
'next_or_number' => 'next' ) ); 

// This shows the page count i.e. "1 of 5"
echo( $page.' of '.count($pages) );

// This shows the Next link
wp_link_pages( array( 'before' => '', 'after' => '</div>', 'previouspagelink' => '', 
'nextpagelink' => 'Next', 'next_or_number' => 'next' ) ); 

?>

This will show Next and Previous Links with the page count in between.

To get your next button to click through to the next post you can possibly further your code with something like this:

<?php
// If the current page equals the last page
if ($page == count($pages)): 
  // Prepare the next post
  // See: https://codex.wordpress.org/Function_Reference/get_next_post
  $next_post = get_next_post(); 
  // Then spit out the next link
  ?>
  <a href="<?php echo get_permalink( $next_post->ID ); ?>">Next</a>
<?php
// End the if statement
endif; ?>
6
  • "Hopefully this will help:" is simply not an answer, nor dumping code. An answer should contain an explanation to what the code provided does, and how it works, and where it should be used. Someone with very little knowledge of wordpress will have no idea what the code is suppose to do, neither won't they know where to add this code. Please edit your answer accordingly to avoid more downvotes from others Commented May 13, 2014 at 5:53
  • Thanks for updating your answer and propely adding additional info. +1 Commented May 13, 2014 at 12:32
  • Great, the first part works great, however I can't get the next article thing to work. Nothing happens with that code. Commented May 13, 2014 at 14:19
  • $pages returns array instead of the value Commented May 13, 2014 at 14:30
  • 1
    fixed changeing $pages to count($pages) Commented May 13, 2014 at 14:31
0

There exists filter: wp_link_pages_args (also, wp_link_pages_link):

add_filter( 'wp_link_pages_args', 'my_function', 11, 1 );
function my_function($base_parameters){
  $new_parameters = array(
        'before'        => '<p class="my_class">' . __( 'Pages:' ),
        'after'         => '</p>',
        'link_before'   => '',  'link_after'    => '',
        'next_or_number'=> 'number',
        'separator'     => ' ',
        'nextpagelink'  => __( 'Next page' ),
        'previouspagelink'=> __( 'Previous page' ),
        'pagelink'      => '%',
        'echo'          => 1
  );
  return $new_parameters;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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