How to change the paginated posts link class?

When using wp_link_pages() to display article pagination, how do I modify the linked class?I want to use different class

When using wp_link_pages() to display article pagination, how do I modify the linked class?

I want to use different classes to set the previous/next link style.

Thanks in advance!

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="/" class="post-page-numbers">PREVIOUS</a>
    <a href="/" class="post-page-numbers">NEXT</a>
</div>

When using wp_link_pages() to display article pagination, how do I modify the linked class?

I want to use different classes to set the previous/next link style.

Thanks in advance!

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/xxxxxx/2/" class="post-page-numbers">PREVIOUS</a>
    <a href="http://test/xxxxxx/4/" class="post-page-numbers">NEXT</a>
</div>
Share Improve this question asked Jul 25, 2019 at 3:58 MatthewMatthew 1515 bronze badges 2
  • If you really must, then there's the wp_link_pages_link filter you can use to modify the class names - in fact, the entire <a> tag. – Sally CJ Commented Jul 25, 2019 at 8:45
  • @SallyCJ How can I achieve this? I am a newbie... – Matthew Commented Jul 25, 2019 at 13:22
Add a comment  | 

1 Answer 1

Reset to default 1

You can use the wp_link_pages_link filter like so:

// 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,
) );

Just change the post-page-numbers prev-page-link and post-page-numbers next-page-link to your liking.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745295810a4621134.html

相关推荐

  • How to change the paginated posts link class?

    When using wp_link_pages() to display article pagination, how do I modify the linked class?I want to use different class

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信