我正在开发的网页设计要求显示上一个和下一个文本,无论是否有实际的上一个或下一个项目要显示。因此,我找到了这篇文章:https://wordpress.stackexchange.com/questions/52638/pagination-how-do-i-always-show-previous
该帖子中的以下代码只要显示即可工作,并手动添加上一个/下一个链接:
$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);
我遇到麻烦的是这段代码:
// 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>';
如何构建实际的上一个/下一个 URL 来替换“#”?
这是我到目前为止整理的代码:
<?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; ?>
希望获得一些帮助,将实际的上一个/下一个 URL 添加到自定义分页。预先感谢!
我今天成功地使用
paginate_links_output
过滤器在标签存档上做到了这一点。以下是 paginate_links 函数输出的一些示例参数:
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] => ''
);
您需要使用上面数组中的
[base]
和 [format]
键构建上一个/下一个按钮。这可以使用字符串替换来实现。您还需要确保第一页和最后一页上的按钮不可单击,并且只需使用 span 元素即可解决。
这是总体目标:
将其添加到您的函数文件中。
<?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__( 'Previous page', 'domain' )
);
} else {
// For pages 2-5, 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 );
我更喜欢过滤器选项,但您可以像上面那样轻松复制代码并手动输入。