我正在做的网页设计要求显示Prev & Next文本,不管是否有实际的上一个或下一个项目要显示。正因为如此,我找到了这篇文章:https://wordpress.stackexchange.com/questions/52638/pagination-how-do-i-always-show-previous
下面的代码来自那篇文章,可以显示并手动添加Prev/Next链接:
$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>';
如何构建实际的prev/next 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; ?>
会喜欢一些帮助添加实际的上一个/下一个网址的自定义分页。提前感谢!
1条答案
按热度按时间nmpmafwu1#
我今天使用
paginate_links_output
过滤器在标签存档中做到了这一点。以下是paginate_links函数输出的一些参数示例:您需要使用上面数组中的
[base]
和[format]
键构建prev/next按钮。这可以通过使用字符串替换来实现。您还需要确保按钮在第一页和最后一页上不可点击,并且只需使用span元素即可解决。以下是总体目标:
1.在第一页上显示“上一页”作为跨度,显示“下一页”作为链接。
1.在最后一页上显示“下一页”作为跨度,显示“上一页”作为链接。
1.对于中间的任何页面,都显示“上一页”和“下一页”作为链接。
把它添加到你的函数文件中。
我更喜欢过滤器选项,但你可以很容易地复制代码,并手动输入它,因为你已经做了上面。