在WordPress中,我使用the_posts_pagination吐出prev / next按钮和页面编号.当我想将某些元素分组为div时,我不喜欢WordPress如何吐出自己的标记.这是我目前的代码:PHP?php the_posts_pagination( array(mid_size...
在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);
}
解决方法:
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()函数,你可能需要更多的参数.
本文标题为:php – 如何将WordPress中the_posts_pagination的页码包装到我自己的div中?
基础教程推荐
- pbootcms二次开发必须要了解的后台目录结构 2023-07-09
- dedecms织梦无需登录注册可下单购买的修改 2022-11-04
- 织梦DedeCMS搜索指定多个栏目文档的办法 2022-11-11
- pbootcms网站自动清理runtime缓存方法 2023-07-09
- dedecms根据来访IP区域自动跳转对应页面的方法 2022-07-21
- dedecms织梦cms常用判断语句汇总 2022-06-24
- PbootCMS网站标题描述等标签限制字数的办法 2023-07-08
- 织梦dedecms调用当前栏目文章数的方法 2022-11-08
- dedecms织梦全局变量调用方法总结 2023-07-08
- 织梦dedecms如何在dede:sql中使用[field:global.autoindex/] 2022-08-27