我想创建一个主题get it approved on wordpres.org 并希望它具有数字页导航:
HTML代码应该引导4的导航组件。
我可以通过自定义函数完成上述操作:
// Numeric Page Navi (built into the theme by default)
function jbst4_page_navi($before = \'\', $after = \'\') {
global $wpdb, $wp_query;
$request = $wp_query->request;
$posts_per_page = intval(get_query_var(\'posts_per_page\'));
$paged = intval(get_query_var(\'paged\'));
$numposts = $wp_query->found_posts;
$max_page = $wp_query->max_num_pages;
if ( $numposts <= $posts_per_page ) { return; }
if(empty($paged) || $paged == 0) {
$paged = 1;
}
$pages_to_show = 7;
$pages_to_show_minus_1 = $pages_to_show-1;
$half_page_start = floor($pages_to_show_minus_1/2);
$half_page_end = ceil($pages_to_show_minus_1/2);
$start_page = $paged - $half_page_start;
if($start_page <= 0) {
$start_page = 1;
}
$end_page = $paged + $half_page_end;
if(($end_page - $start_page) != $pages_to_show_minus_1) {
$end_page = $start_page + $pages_to_show_minus_1;
}
if($end_page > $max_page) {
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = $max_page;
}
if($start_page <= 0) {
$start_page = 1;
}
echo $before.\'<nav style="text-align:center"><ul class="pagination">\'."";
if ($start_page >= 2 && $pages_to_show < $max_page) {
$first_page_text = __( "First", \'jbst-4\' );
echo \'<li class="page-link"><a href="\'.get_pagenum_link().\'" title="\'.$first_page_text.\'">\'.$first_page_text.\'</a></li>\';
}
echo \'<li class="page-item">\';
previous_posts_link(\'<span aria-hidden="true">«</span>
<span class="sr-only">\'. __( \'Previous\', \'jbst-4\' ) .\'</span>\');
echo \'</li>\';
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $paged) {
echo \'<li class="page-item active"><span class="page-link"> \'.$i.\' </span></li>\';
} else {
echo \'<li class="page-item"><a class="page-link" href="\'.get_pagenum_link($i).\'">\'.$i.\'</a></li>\';
}
}
echo \'<li class="page-item">\';
next_posts_link(\'<span aria-hidden="true">»</span>
<span class="sr-only">\'. __( \'Next\', \'jbst-4\' ) .\'</span>\');
echo \'</li>\';
if ($end_page < $max_page) {
$last_page_text = __( "Last", \'jbst-4\' );
echo \'<li class="page-item"><a class="page-link" href="\'.get_pagenum_link($max_page).\'" title="\'.$last_page_text.\'">\'.$last_page_text.\'</a></li>\';
}
echo \'</ul></nav>\'.$after."";
} /* End page navi */
add_filter(\'next_posts_link_attributes\', \'posts_link_attributes_next\');
add_filter(\'previous_posts_link_attributes\', \'posts_link_attributes_previous\');
function posts_link_attributes_next() {
return \'class="page-link" aria-label="\' . __( \'Next\', \'jbst-4\' ) . \'"\';
}
function posts_link_attributes_previous() {
return \'class="page-link" aria-label="\' . __( \'Previous\', \'jbst-4\' ) . \'"\';
}
现在,主题评论员告诉我“使用core the\\u posts\\u navigation()在博客索引上进行页面导航”。
这个the_posts_navigation()
没有给我编号的页码。我找到了the_posts_pagination()
功能。此函数没有用于更改上一个和下一个链接的挂钩或过滤器。我发现我可以用以下代码替换上述函数,其中使用get_the_posts_pagination()
:
<?php
function jbst4_page_navi() {
// Previous/next page navigation.
echo str_replace (
array(\'"next page-numbers"\',\'"previous page-numbers"\'),
array(\'"next page-numbers" aria-label="\' . __( \'Next\', \'jbst-4\' ) . \'"\', \'"previous page-numbers" aria-label="\' . __( \'Previous\', \'jbst-4\' ) . \'"\'),
get_the_posts_pagination( array(
\'prev_text\' => \'<span aria-hidden="true">«</span><span class="sr-only">\'. __( \'Previous\', \'jbst-4\' ) .\'</span>\',
\'next_text\' => \'<span class="page-link" aria-hidden="true">»</span><span class="sr-only">\'. __( \'Next\', \'jbst-4\' ) .\'</span>\',
\'show_all\' => true,
\'add_args\' => array(\'class\' => \'page-item\'),
\'before_page_number\' => \'<span class="page-link">\',
\'after_page_number\' => \'</span>\',
\'screen_reader_text\' => __(\'Page navigation\', \'jbst-4\')
)
)
);
}
/* End page navi */
上述情况并没有改善,我还想知道我现在是否使用WordPress功能?所以我的问题是
the_posts_navigation()
在所有情况下都需要?