[wp-trac] [WordPress Trac] #40108: Pagination Enhancement wp_link_pages()

WordPress Trac noreply at wordpress.org
Sat Mar 11 01:39:02 UTC 2017


#40108: Pagination Enhancement wp_link_pages()
-------------------------+-----------------------------
 Reporter:  mshumacher   |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Themes       |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:  template     |
-------------------------+-----------------------------
 Current implementation of the function is not well suited for posts
 containing 10+ pages, this creates usability issues for desktop and
 especially mobile users. Most popular themes which rely on this built-in
 function are affected. Popular hosting providers such as Wordpress.com do
 not allow 3rd party plugins or any enhancements to work around this issue.

 '''Example:'''
 [[Image(http://i.imgur.com/yqLOfD0.jpg)]]
 '''Goal:'''
 Provide theme creators posibility to reduce the number of visible page
 links and enhance CSS theming by wrapping the current page in CSS
 selectable tag, allowing improved usability on mobile devices and reducing
 the reliance of wider community on 3rd party plugins or custom code.

 '''Solution:'''
 Accept additional 'mixed' value for the [next_or_number] parameter and
 create a helper parameter to control the max number of outputted page
 links. Wrap the current page in a <span> to allow CSS theming.

 The looks/functionality as per the screenshot above can be achieved with
 only ~20 additional lines of code (below)
 It accounts for any edge cases and ensures full backwards compatibility,
 however, additional testing is needed.

 '''Enhanced wp_link_pages Function:'''
 {{{#!php
 <?php
 function wp_link_pages( $args = '' ) {
         global $page, $numpages, $multipage, $more;

         $defaults = array(
                                 /**
                                  *Start of New code for making pagination
 into CSS friendly
                                  */
                 'before'           => '<p>' . __( 'Pages:' ),
                 'after'            => '</p>',
                 'link_before'      => '',
                 'link_after'       => '',
                 'next_or_number'   => 'number',
                 // new param for controling max link number in 'mixed'
 mode
                 'navwidth'         => '3', //number of links displayed
 before and after current page
                 'separator'        => ' ',
                 '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++ ) {
                                 $link = $r['link_before'] . str_replace(
 '%', $i, $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 );
                         }
                                                 /**
                                                  *Start of New Code for
 'mixed' navigation mode
                                                  */

                                                 // Output number of links
 equal to $navwidth before current page
                                                 if ( 'mixed' ==
 $r['next_or_number'] ) {
                                                                 For ( $i =
 $page - $navwidth; $i < $page; $i++) {
 if ( $i > 0) {
 $link = _wp_link_page( $i ) . $r['link_before'] . str_replace( '%', $i,
 $r['pagelink'] ) . $r['link_after'] . '</a>';
 $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
 current page within <span> tags for enhanced styling capability
                                                                 if ( $prev
 ) {
 $output .= $r['separator'];
                                                                 }
                                                                 $output .=
 $r['link_before'] . ('<span>') . str_replace( '%', $i, $r['pagelink'] ) .
 ('</span>') . $r['link_after'];

                                                                 // Output
 number of links equal to $navwidth after current page
                                                                 For ($i =
 $page + 1; $i <= $numpages; $i++) {
 $link = _wp_link_page( $i ) . $r['link_before'] . str_replace( '%', $i,
 $r['pagelink'] ) . $r['link_after'] . '</a>';
 $link = apply_filters( 'wp_link_pages_link', $link, $i );
 $output .= $r['separator'] . $link;
                                                                 }
                                                 }

                                                 /**
                                                  *End of New Code for
 'mixed' navigation mode
                                                  */

                         $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;
 }
 }}}

 Let me know if I can further help to get this be pushed into the next
 release.

 Martin
 [https://martinshreder.com]

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40108>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list