这真的很奇怪。试图解决this problem 最后,我几乎完美地使用了paginate\\u links()而不是自定义分页函数:
$myquery = new WP_Query($args);
$paged = get_query_var(\'page\');
($paged == 0 ? $paged = 1 : $paged = $paged);
$pagination = paginate_links(array(
\'total\' => $myquery->max_num_pages,
\'current\' => $paged,
\'show_all\' => true,
\'type\' => \'list\',
));
这似乎很有效。直到我发现无论我做什么,每当我在第1页之后,函数都会打印一个空的
href=""
在我的第一个链接上。
所以我得到了这些加价:
<!-- base page - example.com/parent-page/child-page/ -->
<!-- same for page 1 - example.com/parent-page/child-page/?page=1 -->
<ul class="page-numbers">
<li><span class="page-numbers current">1</span></li>
<li><a class="page-numbers" href="?page=2">2</li>
<li><a class="page-numbers" href="?page=3">3</li>
<li><a class="page-numbers" href="?page=4">4</li>
<li><a class="next page-numbers" href="?page=2">Next</li>
</ul>
<!-- page 2 - example.com/parent-page/child-page/?page=2 -->
<ul class="page-numbers">
<li><a class="prev page-numbers" href="">Previous</a></li> <!-- empty href="" -->
<li><a class="page-numbers" href="">1</li><!-- empty href="" -->
<li><span class="page-numbers current">2</span></li>
<li><a class="page-numbers" href="?page=3">3</li>
<li><a class="page-numbers" href="?page=4">4</li>
<li><a class="next page-numbers" href="?page=2">Next</li>
</ul>
<!-- page 3 and onwards - example.com/parent-page/child-page/?page=3 -->
<ul class="page-numbers">
<li><a class="prev page-numbers" href="?page=2">Previous</a></li> <!-- correct -->
<li><a class="page-numbers" href="">1</li><!-- empty href="" -->
<li><a class="page-numbers" href="?page=2">2</li>
<li><span class="page-numbers current">3</span></li>
<li><a class="page-numbers" href="?page=4">4</li>
<li><a class="next page-numbers" href="?page=2"></li>
</ul>
深入了解wp includes/general模板上的paginate\\u links()。php(大约1954年的一行)我发现,出于某种原因,它在第2页上显式地将空链接参数传递给前一个链接:
if ( $prev_next && $current && 1 < $current ) :
$link = str_replace(\'%_%\', 2 == $current ? \'\' : $format, $base);
和第一个链接(第1968行)
if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
$link = str_replace(\'%_%\', 1 == $n ? \'\' : $format, $base);
有没有想过为什么会这样,以及是否有可能在不创建另一个分页函数的情况下避免这种情况?