如何更改分页帖子链接类?

时间:2019-07-25 作者:Matthew

使用wp\\u link\\u pages()显示文章分页时,如何修改链接的类?

我想使用不同的类来设置上一个/下一个链接样式。

提前感谢!

Wp_link_pages() parameter:

<?php
wp_link_pages( array(
    \'before\' => \'<div class="page-links">\',
    \'after\'  => \'</div>\',
    \'next_or_number\' => \'next\',
) );
?>

Html output:

<div class="page-links">
    <a href="http://test.com/xxxxxx/2/" class="post-page-numbers">PREVIOUS</a>
    <a href="http://test.com/xxxxxx/4/" class="post-page-numbers">NEXT</a>
</div>

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

您可以使用wp_link_pages_link 像这样过滤:

// Specify the text even if they\'re same as the default. This is for the filter below to work as intended.
$prev_text = \'Previous Page\';
$next_text = \'Next Page\';

add_filter( \'wp_link_pages_link\', function( $link ) use ( $prev_text, $next_text ){
    if ( false !== strpos( $link, $prev_text ) ) { // It\'s a link to the previous page.
        $link = str_replace( \'class="post-page-numbers"\', \'class="post-page-numbers prev-page-link"\', $link );
    }
    elseif ( false !== strpos( $link, $next_text ) ) { // or to the next page.
        $link = str_replace( \'class="post-page-numbers"\', \'class="post-page-numbers next-page-link"\', $link );
    }
    return $link;
} );

wp_link_pages( array(
    \'before\'           => \'<div class="page-links">\',
    \'after\'            => \'</div>\',
    \'next_or_number\'   => \'next\',
    \'previouspagelink\' => $prev_text,
    \'nextpagelink\'     => $next_text,
) );
只需更改post-page-numbers prev-page-linkpost-page-numbers next-page-link 按你的喜好。