Reputation: 1
The web design I'm working on calls for Prev & Next text to be shown regardless if there are actual previous or next items to be shown. Because of that, I found this post: https://wordpress.stackexchange.com/questions/52638/pagination-how-do-i-always-show-previous
The following code from that post works as far as display goes and adds the Prev/Next links in manually:
$page_links = paginate_links(array(
// Make the function never return previous or next links,
// we will add them manually.
'prev_next' => FALSE,
// Give us back an array, this is the easiest to work with.
'type' => 'array',
// + all your other arguments here
));
// Now it is just a matter of adding a previous and next link manually.
// Note: you still have to build the actual prev/next URLs.
$prev_link = '<a href="#">'.__('Previous').'</a>';
$next_link = '<a href="#">'.__('Next').'</a>';
array_unshift($page_links, $prev_link);
$page_links[] = $next_link;
// Output
echo implode($page_links);
Where I'm having trouble is this block of code:
// Now it is just a matter of adding a previous and next link manually.
// Note: you still have to build the actual prev/next URLs.
$prev_link = '<a href="#">'.__('Previous').'</a>';
$next_link = '<a href="#">'.__('Next').'</a>';
How do I build the actual prev/next URLs to replace "#"?
Here is the code I have put together so far:
<?php
if( get_query_var('paged') ) {
$page = get_query_var( 'paged' );
} else {
$page = 1;
}
// Variables
$row = 0;
$press_per_page = 16; // How many images to display on each page
$press_listing = get_field( 'press_info' );
$total = count( $press_listing );
$pages = ceil( $total / $press_per_page );
$min = ( ( $page * $press_per_page ) - $press_per_page ) + 1;
$max = ( $min + $press_per_page ) - 1;
if( have_rows('press_info') ): ?>
<?php while( have_rows('press_info') ): the_row();
$row++;
// Ignore this image if $row is lower than $min
if($row < $min) { continue; }
// Stop loop completely if $row is higher than $max
if($row > $max) { break; }
?>
<div class="press">
<a target="_blank" href="<?php the_sub_field('press_external_link'); ?>"><div class="press-logo"><img src="<?php the_sub_field('press_logo'); ?>"></div>
<div class="press-info">
<p><?php the_sub_field('press_title'); ?></p>
</div></a>
</div>
<?php endwhile;
// Pagination
$page_links = paginate_links(array(
'base' => get_permalink() . 'page/%#%' . '/', 'format' => '?paged=%#%',
'current' => $page,
'total' => $pages,
// Make the function never return previous or next links,
// we will add them manually.
'prev_next' => FALSE,
// Give us back an array, this is the easiest to work with.
'type' => 'array',
// + all your other arguments here
));
// Now it is just a matter of adding a previous and next link manually.
// Note: you still have to build the actual prev/next URLs.
$prev_link = '<div class="the-pagination"><a href="#" class="prev-link">'.__('Prev').'</a>';
$next_link = '<a href="#" class="next-link">'.__('Next').'</a></div>';
array_unshift($page_links, $prev_link);
$page_links[] = $next_link;
// Output
echo implode($page_links);?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Would love some help with adding actual previous / next URLs to the custom pagination. Thanks in advance!
Upvotes: 0
Views: 194
Reputation: 31
I managed to do exactly this on the tag archive using the paginate_links_output
filter today. Here are some example args that are output from the paginate_links function:
Array
(
[base] => https://example.local/tag/example-tag/%_%
[format] => page/%#%/
[total] => 2
[current] => 1
[aria_current] => page
[show_all] =>
[prev_next] =>
[prev_text] => « Previous
[next_text] => Next »
[end_size] => 1
[mid_size] => 2
[type] => plain
[add_args] => Array
(
)
[add_fragment] => ''
[before_page_number] => ''
[after_page_number] => ''
);
You will need to build up the prev/next buttons using the [base]
and [format]
keys in the array above. This can be achieved using a string replace. You will also need to ensure that the buttons are not clickable on the first and last pages and solved simply by using a span element.
Here is the general goal:
Add this to your functions file.
<?php
function so_74440851_modify_query_pagination( $output, $args ) {
if ( 1 === $args['current'] ) {
// Show "Previous" as a span and "Next" as a link on the first page.
$next_page = $args['current'] + 1;
$next_url = str_replace( '%_%', 'page/' . $next_page, $args['base'] );
$next_url = str_replace( '%#%', $next_page, $next_url );
$prev = sprintf(
'<span class="%s">%s</span>',
esc_attr( 'archive-pagination-button archive-pagination-button-inactive' ),
esc_html__( 'Previous page', 'domain' )
);
$next = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url( $next_url ),
esc_attr( 'archive-pagination-button' ),
esc_html__( 'Next page', 'domain' )
);
} elseif ( intval( $args['total'] ) === $args['current'] ) {
// Show "Next" as a span and "Previous" as a link on the last page.
$prev_page = $args['current'] - 1;
$prev_url = str_replace( '%_%', 'page/' . $prev_page, $args['base'] );
$prev_url = str_replace( '%#%', $prev_page, $prev_url );
$prev = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url( $prev_url ),
esc_attr( 'archive-pagination-button' ),
esc_html__( 'Previous page', 'domain' )
);
$next = sprintf(
'<span class="%s">%s</span>',
esc_attr( 'archive-pagination-button archive-pagination-button-inactive' ),
esc_html__( 'Next page', 'domain' )
);
} else {
// For pages in between, show both "Previous" and "Next" as links.
$prev_page = $args['current'] - 1;
$next_page = $args['current'] + 1;
$prev_url = str_replace( '%_%', 'page/' . $prev_page, $args['base'] );
$prev_url = str_replace( '%#%', $prev_page, $prev_url );
$next_url = str_replace( '%_%', 'page/' . $next_page, $args['base'] );
$next_url = str_replace( '%#%', $next_page, $next_url );
$prev = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url( $prev_url ),
esc_attr( 'archive-pagination-button' ),
esc_html__( 'Previous page', 'domain' )
);
$next = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url( $next_url ),
esc_attr( 'archive-pagination-button' ),
esc_html__( 'Next page', 'domain' )
);
}
$output = $prev . "\n" . $output . "\n" . $next;
return $output;
}
add_filter( 'paginate_links_output', 'so_74440851_modify_query_pagination', 10, 2 );
I prefer the filter option but you could just as easily copy the code and enter it manually as you have done above.
Upvotes: 0