自定义“The_Posts_Pagination”并将列表放入div

时间:2020-12-01 作者:GilbertOOl

Hello everybody, 我需要有关自定义wordpress分页的帮助。

我想得到这个:

enter image description here

这就是我所做的:

在函数中。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\', \'
    <div class="col-auto">
        <a href="" class="btn btn-outline-white text-dark">%3$s</a>
    </div>
    <div class="col-auto">
        <nav class="navigation %1$s" role="navigation">
                <h2 class="screen-reader-text">%2$s</h2>
                <ul class="nav-links pagination mb-0 text-dark">
                    <li class="page-item">%4$s</li>
                </ul>
        </nav>
    </div>
    <div class="col-auto">
        <a href="" class="btn btn-outline-white text-dark">%5$s</a>
    </div>\', $args, $class);

    echo sprintf($template, $class, $args[\'screen_reader_text\'], $prev_link, $links, $next_link);}
之后,我在家中添加了此代码。php

<?php wp_custom_pagination(); ?>
但是,结果真的不是我想要的!

以下是此wp\\u custom\\u分页所需的html结构代码:

            <div class="row justify-content-between align-items-center">
              <div class="col-auto">
                <a href="#" class="btn btn-outline-white text-dark">Previous</a>
              </div>
              <div class="col-auto">
                <nav>
                  <ul class="pagination mb-0 text-dark">
                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                  </ul>
                </nav>
              </div>
              <div class="col-auto">
                <a href="#" class="btn btn-outline-white text-dark">Next</a>
              </div>
            </div>
如果有人能帮助我,告诉我,告诉我我的错误,以及如何得到我想要的,请。

非常感谢

此时,我得到的结果是:

enter image description here

这是我的自定义分页页面的生成代码:

<div class="row justify-content-between align-items-center">
    <div class="col-auto">
        <a href="" class="btn btn-outline-white text-dark"></a>
        <a href="http://192.168.1.87/wordpress/blog/page/2">Older posts</a>
    </div>
    <div class="col-auto">
        <nav class="navigation pagination" role="navigation">
            <h2 class="screen-reader-text">Posts navigation</h2>
            <ul class="nav-links pagination mb-0 text-dark">
                <li class="page-item">
                    <span aria-current="page" class="page-numbers current">1</span>
                    <a class="page-numbers" href="http://192.168.1.87/wordpress/blog/page/2">2</a>
                </li>
            </ul>
        </nav>
    </div>
    <div class="col-auto">
        <a href="" class="btn btn-outline-white text-dark"></a>
    </div>
</div>
因此,如果你看看结果:

1-您将看到第一个链接(较旧的帖子=%3$s)需要使用好的css类!

2-您将看到;1、2、3“;链接需要在每个链接中”;“li”;不在一个内“;“li”;

2 个回复
最合适的回答,由SO网友:HK89 整理而成

在演示中根据需要定制分页设计。

以下函数替换您的函数:

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\'                  => __(\'Previous\', \'textdomain\'),
        \'next_text\'                  => __(\'Next\', \'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\']);
$check_prev = (!empty($prev_link))?$prev_link:\'Previous\';
$check_next = (!empty($next_link))?$next_link:\'Next\';
$template    = apply_filters( \'navigation_markup_template\', \'
<div class="container"><div class= "row"><div style="display: flex;justify-content: space-around; align-items: center;" class="paginatin d-flex justify-content-between align-items-center"><div class="col-auto">
    <button type="button" class="btn btn-outline-info" >%3$s</button>
</div>
<div class="col-auto">
    <nav class="navigation %1$s" role="navigation">
            <h2 class="screen-reader-text">%2$s</h2>
            <ul class="pagination mb-0 text-dark">
                <li class="page-item">%4$s</li>
            </ul>
    </nav>
</div>
<div class="col-auto">
   <button type="button" class="btn btn-outline-info">%5$s</button>
</div></div></div><div>\', $args, $class);

echo sprintf($template, $class, $args[\'screen_reader_text\'], $check_next, $links, $check_prev);

}
在标题中添加一些Css:

function add_css_pagination(){
?>  
    <style type="text/css">
        span.page-numbers.current {
            background: #a8307c;
            color: white;
        }
        button.btn a {
            color: #f4f4f4;
            text-decoration: none;
        }
        button.btn.btn-outline-info a {
            color: darkslategrey;
            font-weight: 600;
        }
        button.btn.btn-outline-info {
            background: unset;
            border: 1px solid turquoise;
            color: darkslategrey;
            font-weight: 600;
        }
    </style>

 <?php
 }
 add_action(\'wp_head\',\'add_css_pagination\');
屏幕截图:enter image description here

SO网友:Bunty

而不是the_posts_pagination( wp_custom_pagination () );, 只需使用此功能paginate_links 并根据需要添加参数。

示例:

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $the_query->max_num_pages,
    \'prev_text\' => __( \'Previous\' ),
    \'next_text\' => __( \'Next\' ),
) );
参考号:https://developer.wordpress.org/reference/functions/paginate_links/

相关推荐

pagination leads to 404 page

我想弄明白为什么我的分页没有显示出来,我快发疯了。我将设置场景:我有一个页面“category.php”。在这个页面中,我为所有分类帖子设置了一个自定义查询。它们是自定义帖子类型,并显示该类别的自定义帖子。我的问题如下:我已经尝试过这个函数:the_post_pagination();$category = get_category(get_query_var(\'cat\')); if ( get_query_var(\'paged\') ) { $paged = get_query_var(