是否需要使用_POSTS_NAVATION()?

时间:2016-05-29 作者:Bass Jobsen

我想创建一个主题get it approved on wordpres.org 并希望它具有数字页导航:

enter image description here

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">&laquo;</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">&raquo;</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">&laquo;</span><span class="sr-only">\'. __( \'Previous\', \'jbst-4\' ) .\'</span>\',
  \'next_text\'          => \'<span class="page-link" aria-hidden="true">&raquo;</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() 在所有情况下都需要?

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

您要使用的分页类型当前被视为“插件区域”。下载您批准的主题的用户不得强制使用它。例如,他们可能喜欢另一种分页系统。因此,主题必须具有以下基本分页the_posts_navigation() 作为标准。这是关于确保主题用户的灵活性。

正如@majick所建议的,您可以尝试将分页作为一个选项,但正式的方法是将分页构建到插件中,并建议用户将其与主题一起安装。

相关推荐

如何创建4列和2列Twitter-Bootstrap相结合的WordPress循环?

我想根据4列(桌面视图)、2列(mobile view 768px)和1列(mobile view 425px)的组合显示帖子。我在下面找到了很棒的代码:<?php $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1; $args=array( \'post_type\' => \'post\',