Forums

[resolved] Styling Page-Links look like: < 1/3 > (5 posts)

  1. Lukree
    Member
    Posted 2 years ago #

    I have tried to search for the answer on this one but I still haven't found how I could achieve the following inside paged posts.

    I have split posts to multiple parts with <!–nextpage–> -tag and I'm trying to style post navigation to look like following:

    < 1/3 >

    Arrows for previous and next page inside the post and changing number showing current page and total number of pages.

    It doesn't seem to be able to make this with wp_link_pages as you can only select either numbers (1 2 3 4 5) or links (previous_page next_page) to navigate the content.

    Can someone help me? Thanks in advance!

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Copy the code below and paste at the end of your theme's functions.php.
    Then call my_link_pages() instead of wp_link_pages(). This version takes no parameters. Tested on WP version 2.8.6.

    <?php function my_link_pages() {
       global $post, $page, $multipage, $numpages, $more, $pagenow;
    
       if (!$multipage) {
          echo '';
          return;
       }
       $links = wp_link_pages(array('echo' => 0, 'link_before' => ':', 'link_after' => ':', 'before' => '', 'after' => '', 'next_or_number' => 'number'));
       $output = '';
       if ($links) {
          //print_r('<p>LINKS:');print_r(htmlspecialchars($links));print_r(':::</p>');
          $parts = explode('<a ',$links);
          //print_r('<p>PARTS:');print_r(htmlspecialchars($parts[1]));print_r('</p>');
    
          // The part with a trailing number is the previous link followed by current page number
          $prev_link_ndx = -1;
          for ($i=0;$i<$numpages;++$i) {
             if (preg_match('/ :(\d+): *$/', $parts[$i], $matches)) $prev_link_ndx = $i;
          }
          //print_r('<p>NDX:');print_r($prev_link_ndx);print_r('</p>');
          if (prev_link_ndx > -1) {
             if ($prev_link_ndx == 0) {
                $prev_link = '< 1/' . $numpages;
             } else {
             $prev_link = preg_replace('/ :(\d+): *$/',' $1/'.$numpages.' ',$parts[$prev_link_ndx]);
             $prev_link = '<a ' . preg_replace('/:\d+:/','<',$prev_link);
             }
             if ($prev_link_ndx < $numpages - 1) {
                $next_link = '<a ' . preg_replace('/:\d+:/',' >',$parts[$prev_link_ndx + 1]);
             } else {
                $next_link = ' >';
             }
             //print_r('<p>PREV:');print_r(htmlspecialchars($prev_link));print_r('</p>');
             //print_r('<p>NEXT:');print_r(htmlspecialchars($next_link));print_r('</p>');
             $output =  "<p>$prev_link$next_link</p>";
          }
          echo $output;
       }
    }
    ?>
  3. Lukree
    Member
    Posted 2 years ago #

    Haven't tested it yet, but thanks a million, vtxyzzy!

  4. Lukree
    Member
    Posted 2 years ago #

    For some reason this ruined my custom page listings when placed at the end of functions.php (version 2.9.1), but worked nicely when placed closer to the top!

    So now it works better than Titanic, thanks again!

  5. vtxyzzy
    Member
    Posted 2 years ago #

    You are welcome.

Topic Closed

This topic has been closed to new replies.

About this Topic