Community Forums › Forums › Archived Forums › Design Tips and Tricks › Function to modify numbered pagination
Tagged: archive pagination, pagination
- This topic has 20 replies, 3 voices, and was last updated 9 years ago by Carla the Moose.
-
AuthorPosts
-
September 29, 2015 at 2:48 pm #166968Carla the MooseMember
I'm making some headway on this. At least I think I'm heading in the right direction, but I welcome your feedback.
I want to control how many pagination links display at the bottom of my blog. It seems 7 page links display at the most, but I would like to only show five and remove the previous/next links. I can remove the text, but the blank space is still a link.
Presently it's this: 1 ... 22 23 24 25 26 ... 48
But I would like to modify it to display like this: 1 ... 23 24 25 ... 48
I've been working with this from the codex and am using my rusty skills to create a function:
https://codex.wordpress.org/Function_Reference/paginate_links
Please let me know if I'm on the right track. I think I need to add a filter. Filters let you modify an existing function, right? So then I would write a function after the filter and add the code from the codex.
My site isn't live yet.
Thank you.
September 29, 2015 at 3:05 pm #166973Brad DaltonParticipantHave you tried grabbing the code from genesis and modifying it in your child theme?
/** * Echo archive pagination in page numbers format. * * Applies the
genesis_prev_link_text
andgenesis_next_link_text
filters. * * The links, if needed, are ordered as: * * * previous page arrow, * * first page, * * up to two pages before current page, * * current page, * * up to two pages after the current page, * * last page, * * next page arrow. * * @since 0.2.3 * * @global WP_Query $wp_query Query object. * * @return null Return early if on a single post or page, or only one page present. */ function genesis_numeric_posts_nav() { if( is_singular() ) return; global $wp_query; //* Stop execution if there's only 1 page if( $wp_query->max_num_pages <= 1 ) return; $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = intval( $wp_query->max_num_pages ); //* Add current page to the array if ( $paged >= 1 ) $links[] = $paged; //* Add the pages around the current page to the array if ( $paged >= 3 ) { $links[] = $paged - 1; $links[] = $paged - 2; } if ( ( $paged + 2 ) <= $max ) { $links[] = $paged + 2; $links[] = $paged + 1; } genesis_markup( array( 'html5' => '<div %s>', 'xhtml' => '<div class="navigation">', 'context' => 'archive-pagination', ) ); $before_number = genesis_a11y() ? '<span class="screen-reader-text">' . __( 'Page ', 'genesis' ) . '</span>' : ''; echo '- ';
//* Previous Post Link
if ( get_previous_posts_link() )
printf( '<li class="pagination-previous">%s' . "\n", get_previous_posts_link( apply_filters( 'genesis_prev_link_text', '« ' . __( 'Previous Page', 'genesis' ) ) ) );
//* Link to first page, plus ellipses if necessary
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), $before_number . '1' );
if ( ! in_array( 2, $links ) )
echo '<li class="pagination-omission">…';
}
//* Link to current page, plus 2 pages in either direction if necessary
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active" aria-label="' . __( 'Current page', 'genesis' ) . '"' : '';
printf( '<li%s>%s' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $before_number . $link );
}
//* Link to last page, plus ellipses if necessary
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li class="pagination-omission">…' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $before_number . $max );
}
//* Next Post Link
if ( get_next_posts_link() )
printf( '<li class="pagination-next">%s' . "\n", get_next_posts_link( apply_filters( 'genesis_next_link_text', __( 'Next Page', 'genesis' ) . ' »' ) ) );
echo '
September 29, 2015 at 3:17 pm #166977Carla the MooseMemberFrom what I was able to find, it seems the code is in the includes file, not Genesis. There's a links file where the pagination is listed. But I couldn't make sense of what I was seeing. I couldn't find anything that looked similar to what I found in the codex.
I browsed Genesis and didn't see anything. I'll take another look.
September 29, 2015 at 3:19 pm #166978Carla the MooseMemberMost of the Genesis files are largely empty and call other files I can't find.
September 29, 2015 at 3:19 pm #166979Brad DaltonParticipantCreate and hook in the new function and remove the default.
There's a bit of work in it but thats how i would do it.
September 29, 2015 at 3:24 pm #166980Brad DaltonParticipantI posted the code here and added a link to the Gist but its been stripped out.
September 29, 2015 at 3:26 pm #166981Carla the MooseMemberI've never written my own function before, but thanks for getting me started. I didn't look hard enough in the files.
I was able to see the link and code, thank you. I'm not sure why it disappeared.
September 29, 2015 at 4:03 pm #166983Carla the MooseMemberHere's how far I got so far.
I tried removing the function to replace it with the modified one. It didn't go away. So I figured I'll deal with that later. Just to show myself I'm capable of at least doing something easy, I added the pagination to the header. It's temporary.
I copied the code from the codex and nothing died. No white screens. But nothing has changed, either. The only change I did, as a test, was to add hello to the previous text. It doesn't show up. So nothing died, but nothing was rebirthed, either.
Genesis must be using different names or code. I can't just paste that section from post.php into my functions file. That's where things turned white and died. So I have to write a function. Any tips on what to do next?
//* MODIFY NUMBERED PAGINATION add_action( 'genesis_header', 'genesis_numeric_posts_nav' ); add_filter( 'genesis_numeric_posts_nav', 'sp_numeric_posts_nav' ); function sp_numeric_posts_nav() { echo paginate_links( array( 'base' => '%_%', 'format' => '?page=%#%', 'total' => 1, 'current' => 0, 'show_all' => False, 'end_size' => 1, 'mid_size' => 2, 'prev_next' => True, 'prev_text' => __('« hello'), 'next_text' => __('Next »'), 'type' => 'plain', 'add_args' => False, 'add_fragment' => '', 'before_page_number' => '', 'after_page_number' => '' ) ); }
September 29, 2015 at 4:05 pm #166984Brad DaltonParticipantSeptember 29, 2015 at 4:08 pm #166986Carla the MooseMemberWell, if I weren't broke I'd certainly pay for access to your blog post.
I'll keep plugging away on this. Thank you.
September 29, 2015 at 4:32 pm #166988Carla the MooseMemberNow I'm this far. And once again, nothing white screened at me. Things are still alive.
My test is changing the previous page text to hello page. When I see hello, I'll know I'm getting there. And next page is now doggy page. This is just for testing purposes.
So far, it's not working. It must have something to do with the filter or not removing the default or something along those lines. I'm getting there, though.
//* MODIFY NUMBERED PAGINATION add_filter( 'genesis_numeric_posts_nav', 'sp_numeric_posts_nav' ); function sp_numeric_posts_nav() { if( is_singular() ) return; global $wp_query; //* Stop execution if there's only 1 page if( $wp_query->max_num_pages <= 1 ) return; $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = intval( $wp_query->max_num_pages ); //* Add current page to the array if ( $paged >= 1 ) $links[] = $paged; //* Add the pages around the current page to the array if ( $paged >= 3 ) { $links[] = $paged - 1; $links[] = $paged - 2; } if ( ( $paged + 2 ) <= $max ) { $links[] = $paged + 2; $links[] = $paged + 1; } genesis_markup( array( 'html5' => '<div %s>', 'xhtml' => '<div class="navigation">', 'context' => 'archive-pagination', ) ); $before_number = genesis_a11y() ? '<span class="screen-reader-text">' . __( 'Page ', 'genesis' ) . '</span>' : ''; echo '<ul>'; //* Previous Post Link if ( get_previous_posts_link() ) printf( '<li class="pagination-previous">%s</li>' . "\n", get_previous_posts_link( apply_filters( 'genesis_prev_link_text', '« ' . __( 'hello Page', 'genesis' ) ) ) ); //* Link to first page, plus ellipses if necessary if ( ! in_array( 1, $links ) ) { $class = 1 == $paged ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), $before_number . '1' ); if ( ! in_array( 2, $links ) ) echo '<li class="pagination-omission">…</li>'; } //* Link to current page, plus 2 pages in either direction if necessary sort( $links ); foreach ( (array) $links as $link ) { $class = $paged == $link ? ' class="active" aria-label="' . __( 'Current page', 'genesis' ) . '"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $before_number . $link ); } //* Link to last page, plus ellipses if necessary if ( ! in_array( $max, $links ) ) { if ( ! in_array( $max - 1, $links ) ) echo '<li class="pagination-omission">…</li>' . "\n"; $class = $paged == $max ? ' class="active"' : ''; printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $before_number . $max ); } //* Next Post Link if ( get_next_posts_link() ) printf( '<li class="pagination-next">%s</li>' . "\n", get_next_posts_link( apply_filters( 'genesis_next_link_text', __( 'doggy Page', 'genesis' ) . ' »' ) ) ); echo '</ul></div>' . "\n"; }
September 29, 2015 at 4:38 pm #166990emmtreParticipantYou shouldn't modify the Genesis files but here is what I did. Don't remember if I had to change anything else.
September 29, 2015 at 4:42 pm #166991Carla the MooseMemberThanks, I'll explore that discussion.
I'm doing this in my child theme.
September 29, 2015 at 4:48 pm #166992Carla the MooseMemberI've now managed to remove the dang function, which is good. It's like learning to crawl before you stand up and walk.
The problem was, I was using genesis_numeric_posts_nav instead of genesis_posts_nav.
It all looks the same in the post.php file. But now it's glad to be properly removed when called by its proper name.
September 29, 2015 at 7:43 pm #167009Carla the MooseMemberBrad, if you're seeing this, have you come up with a way to modify numbered pagination for mobile devices? I much prefer numbered page links over "next" and "previous" links. But they break onto a second line in smaller windows.
I'm still hammering out the code for my website. The five links I want displayed won't break in small windows, but lots of people display the full amount, and that can split onto another line depending on how much content they have.
September 29, 2015 at 8:38 pm #167014Carla the MooseMemberAck, the brain is hurting.
So I did a major no-no and worked directly with the post.php file in Genesis. I couldn't get the function to work because I wasn't understanding the core function. My site hasn't launched, and I made a copy of this file in case I messed up.
The end result:
I reduced the number of page links that display on either side of the current page, allowing for no more than 5 page links in total. I also removed the previous and next text links. You have to remove the entire code or it leaves behind a text-less hyperlinked space you'll discover when you hover over it.
Now, I need to work on learning how to write a function.
September 30, 2015 at 5:20 am #167034Brad DaltonParticipantHappy to point you in the right direction. You should find the function already included in genesis.
September 30, 2015 at 3:11 pm #167085Carla the MooseMemberI've been a fan of yours for years, Brad. You've helped me a number of times.
:o)
October 1, 2015 at 4:47 am #167127Brad DaltonParticipantOctober 1, 2015 at 1:49 pm #167173emmtreParticipantWoodlandCoyote, do you mind posting the solution?
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.