I'm new to WordPress Development and have been teaching myself over the last few months how to develop a theme from scratch. Normally I can figure issues out on my own through forums, but I can't really find much on this one.
I've been trying to add leading zeros to my pagination if the number is less than 10.
I.e: < Newer 01 02 03 ... 10 Older >
Here is my pagination code. Any pointers in the right direction would be appreciated!
if ( !function_exists('palfrey_pagination') ) {
function palfrey_pagination( $range = 5 ) {
if( is_singular() )
return;
// $paged - number of the current page
global $paged, $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;
if ( !$max_page )
$max = intval( $wp_query->max_num_pages );
if ( $max_page > 1 )
if ( !$paged ) $paged = 1;
// 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;
}
// The pagination
echo "\n" . '<div class="content-block-common-large">
<div class="block-wrap">
<div class="inline-group flex-group relative align-center column-align-bottom">
<div class="column responsive width-450">
<ul class="pagination display-block relative align-center width-1of1">' . "\n";
// Link to 'Newer' posts.
if ( get_previous_posts_link() ) {
printf( '<li>%s</li>' . "\n", get_previous_posts_link( '<div class="display-inline-block relative float-left"><span class="pagination-prev">Newer</span></div>' ) );
}else{
echo '<li><div class="display-inline-block relative float-left pointer-events-none" style="opacity: .6;"><span class="pagination-prev">Newer</span></div></li>';
}
// 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 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</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"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link) ), $link );
}
// Link to last page, plus ellipses if necessary.
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max) ), $max );
}
// Link to 'Older' posts.
if ( get_next_posts_link() ) {
printf( '<li>%s</li>' . "\n", get_next_posts_link( '<div class="display-inline-block relative float-right"><span class="pagination-next">Older</span></div>' ) );
}else{
echo '<li><div class="display-inline-block relative float-right pointer-events-none" style="opacity: .6;"><span class="pagination-next">Older</span></div></li>';
}
echo "\n" . ' </ul>
</div>
</div>
</div>
</div>' . "\n";
}
}
I'm new to WordPress Development and have been teaching myself over the last few months how to develop a theme from scratch. Normally I can figure issues out on my own through forums, but I can't really find much on this one.
I've been trying to add leading zeros to my pagination if the number is less than 10.
I.e: < Newer 01 02 03 ... 10 Older >
Here is my pagination code. Any pointers in the right direction would be appreciated!
if ( !function_exists('palfrey_pagination') ) {
function palfrey_pagination( $range = 5 ) {
if( is_singular() )
return;
// $paged - number of the current page
global $paged, $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;
if ( !$max_page )
$max = intval( $wp_query->max_num_pages );
if ( $max_page > 1 )
if ( !$paged ) $paged = 1;
// 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;
}
// The pagination
echo "\n" . '<div class="content-block-common-large">
<div class="block-wrap">
<div class="inline-group flex-group relative align-center column-align-bottom">
<div class="column responsive width-450">
<ul class="pagination display-block relative align-center width-1of1">' . "\n";
// Link to 'Newer' posts.
if ( get_previous_posts_link() ) {
printf( '<li>%s</li>' . "\n", get_previous_posts_link( '<div class="display-inline-block relative float-left"><span class="pagination-prev">Newer</span></div>' ) );
}else{
echo '<li><div class="display-inline-block relative float-left pointer-events-none" style="opacity: .6;"><span class="pagination-prev">Newer</span></div></li>';
}
// 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 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</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"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link) ), $link );
}
// Link to last page, plus ellipses if necessary.
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max) ), $max );
}
// Link to 'Older' posts.
if ( get_next_posts_link() ) {
printf( '<li>%s</li>' . "\n", get_next_posts_link( '<div class="display-inline-block relative float-right"><span class="pagination-next">Older</span></div>' ) );
}else{
echo '<li><div class="display-inline-block relative float-right pointer-events-none" style="opacity: .6;"><span class="pagination-next">Older</span></div></li>';
}
echo "\n" . ' </ul>
</div>
</div>
</div>
</div>' . "\n";
}
}
Share
Improve this question
asked Jul 17, 2020 at 0:46
FaulineFauline
212 bronze badges
1 Answer
Reset to default 1When your code gets to the point of outputing a number, adjust the number ($pagenumber
in this code example) to include leading spaces with a variation on this
$pagenumber = 123;
echo sprintf("%'.09d\n", $pagenumber);
will output
000000123
See the manual: https://www.php/manual/en/function.sprintf.php , example 2, and other examples in there.