如何将WordPress中the_posts_pagination的页码包装到我自己的div中?

时间:2016-06-02 00:45:03

标签: php html wordpress

在WordPress中,我使用the_posts_pagination吐出prev / next按钮和页码。当我想将某些元素分组为div时,我不喜欢WordPress如何吐出自己的标记。

这是我目前的代码:

PHP

<?php the_posts_pagination( array(
   'mid_size' => 2,
   'prev_text' => __( 'Previous Page', 'textdomain' ),
   'next_text' => __( 'Next Page', 'textdomain' ),
   'screen_reader_text' => ( '' )
) ); ?> 

它吐出了这个:

HTML

<nav class="navigation pagination" role="navigation">
        <div class="nav-links">
        <a class="prev page-numbers" href="#">Prev Page</a>
        <a class="page-numbers" href="#">1</a>
        <span class="page-numbers current">2</span>
        <a class="page-numbers" href="">3</a>
        <a class="page-numbers" href="">4</a>
        <a class="next page-numbers" href="">Next Page</a>
    </div>
</nav>

我希望能够让它看起来像这样:

<nav class="navigation pagination" role="navigation">
        <div class="nav-links">
        <a class="prev page-numbers" href="#">Prev Page</a>
        <div class="page-numbers-container">
             <a class="page-numbers" href="#">1</a>
             <span class="page-numbers current">2</span>
             <a class="page-numbers" href="">3</a>
             <a class="page-numbers" href="">4</a>
        </div>
        <a class="next page-numbers" href="">Next Page</a>
    </div>
</nav>

即使我可以将div中的下一个和上一个链接包装起来也很棒。我不想编辑核心WordPress文件中的任何内容。我需要写一个函数吗?

修改

这是在我的index.php

<?php the_posts_pagination( wp_custom_pagination(['prev_text' => __( 'Previous Page', 'textdomain' ), 'next_text' => __( 'Next Page', 'textdomain' )])); ?>

这是我放在functions.php

中的内容
function wp_custom_pagination($args = [], $class = 'pagination') {

            if ($GLOBALS['wp_query']->max_num_pages <= 1) return;

            $args = wp_parse_args( $args, [
                'mid_size'           => 2,
                'prev_next'          => false,
                'prev_text'          => __('Older posts', 'textdomain'),
                'next_text'          => __('Newer posts', 'textdomain'),
                'screen_reader_text' => __('Posts navigation', 'textdomain'),
            ]);

            $links     = paginate_links($args);
            $next_link = get_previous_posts_link($args['next_text']);
            $prev_link = get_next_posts_link($args['prev_text']);
            $template  = apply_filters( 'navigation_markup_template', '
            <nav class="navigation %1$s" role="navigation">
                <h2 class="screen-reader-text">%2$s</h2>
                <div class="nav-links">%3$s<div class="page-numbers-container">%4$s</div>%5$s</div>
            </nav>', $args, $class);

            echo sprintf($template, $class, $args['screen_reader_text'], $prev_link, $links, $next_link);

        }

1 个答案:

答案 0 :(得分:7)

the_posts_pagination()使用paginate_links()没有过滤器来根据需要更改输出。

因此,让我们创建一个自定义分页模板:

function the_so37580965_wp_custom_pagination($args = [], $class = 'pagination') {

    if ($GLOBALS['wp_query']->max_num_pages <= 1) return;

    $args = wp_parse_args( $args, [
        'mid_size'           => 2,
        'prev_next'          => false,
        'prev_text'          => __('Older posts', 'textdomain'),
        'next_text'          => __('Newer posts', 'textdomain'),
        'screen_reader_text' => __('Posts navigation', 'textdomain'),
    ]);

    $links     = paginate_links($args);
    $next_link = get_previous_posts_link($args['next_text']);
    $prev_link = get_next_posts_link($args['prev_text']);
    $template  = apply_filters( 'the_so37580965_navigation_markup_template', '
    <nav class="navigation %1$s" role="navigation">
        <h2 class="screen-reader-text">%2$s</h2>
        <div class="nav-links">%3$s<div class="page-numbers-container">%4$s</div>%5$s</div>
    </nav>', $args, $class);

    echo sprintf($template, $class, $args['screen_reader_text'], $prev_link, $links, $next_link);

}

试一试。另外,看看paginate_links()函数,还有更多你可能需要的参数。