0

I want to create a theme and get it approved on wordpres.org and want it having number page navigation:

enter image description here

The HTML code should Bootstrap 4's navigation component.

i can accomplish the above with an custom function:

// 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' ) . '"';
} 

Now the theme reviewer tells me "use core the_posts_navigation() for page navigation on your blog index".

the the_posts_navigation() does not give me a numbered pages. I found the the_posts_pagination() functions. This function does not have hooks or filters to change the previous and and next links. I found that i can replace the above function with the following code, which uses 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 */

The above does not feel as an improvement, i'm also wonder if i do use WordPress features now? So my question is using the_posts_navigation() required in all situations?

  • 2
    The answer is "no", but the theme review team has their own policy, you should talk with the reviewer instead of asking here. AFAIK no theme reviewer is actively participating in the site so very slim chance of getting a good answer here. – Mark Kaplun May 30 '16 at 5:32
  • I'd suggest using the default requirement as suggested and adding a user option to use the bootstrap implementation instead... as far as I understand it the wp.org theme guidelines are there to help set a default standard - but that does not mean - and should not mean - that you can't include options that you feel improve upon that standard. – majick May 30 '16 at 5:42
1

The type of pagination you want to use is currently considered 'plugin territory'. Users that download your approved theme must not be forced to use it. They may, for instance, have another system of pagination which they prefer. Therefore a theme must have the barebones pagination of the_posts_navigation() as a standard. It's about ensuring flexibility to your theme's users.

You may try to include your pagination as an option, as @majick suggests, but the formal approach would be to build your pagination into a plugin and recommend your users to install it with your theme.

| improve this answer | |
  • okay i understand. But i can be done with get_the_posts_pagination without a plugin. But get_the_posts_pagination does not have any hooks / like next_posts_link_attributes. Improving get_the_posts_pagination make more sense for me than simply saying it is 'plugin territory'. – Bass Jobsen May 30 '16 at 21:29
  • 1
    Of course it can be done with get_the_posts_pagination. That is how plugins do it. And if it can be done in a plugin, it can be done in a theme (and the other way around). Surely it is debatable whether pagination is functionality (plugin) or design (theme). It's just that by maintaing standards about what belongs where WP tries to manage expectations of users and developers. What makes sense to you or me, may make no sense to somebody else. – cjbj May 31 '16 at 7:25
  • 1
    It's a bit like with you other question yesterday: surely you can get around the standard and it will work fine at that point in time, but you open your code up to risks because the guys at WP only support standard code. When they change the core code they will provide fallbacks for code that stuck to former standards, but not to other code. In this case, I think the risk is very small, by the way. – cjbj May 31 '16 at 7:37
  • Notice that also the TwentySixteen theme uses the_posts_pagination by default, so i do think numbered page navigations should not be consider plugin territory. The problem here is that the the_posts_pagination is very limited, see also themes.trac.wordpress.org/ticket/29732#comment:28 – Bass Jobsen Jun 13 '16 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.