wordpress 为paginate_links创建实际的上一个/下一个URL

46qrfjad  于 2023-10-16  发布在  WordPress
关注(0)|答案(1)|浏览(138)

我正在做的网页设计要求显示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; ?>

会喜欢一些帮助添加实际的上一个/下一个网址的自定义分页。提前感谢!

nmpmafwu

nmpmafwu1#

我今天使用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]键构建prev/next按钮。这可以通过使用字符串替换来实现。您还需要确保按钮在第一页和最后一页上不可点击,并且只需使用span元素即可解决。
以下是总体目标:
1.在第一页上显示“上一页”作为跨度,显示“下一页”作为链接。
1.在最后一页上显示“下一页”作为跨度,显示“上一页”作为链接。
1.对于中间的任何页面,都显示“上一页”和“下一页”作为链接。
把它添加到你的函数文件中。

<?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 );

我更喜欢过滤器选项,但你可以很容易地复制代码,并手动输入它,因为你已经做了上面。

相关问题