WordPress.org

Ready to get started?Download WordPress

Forums

WP-PageNavi
Options > Number Of Pages To Show — no 'auto' option? (2 posts)

  1. Barney Carroll
    Member
    Posted 3 years ago #

    As a brief disclaimer, I followed the instructions on this page to make PageNavi play well with the Magazeen theme. The net effect of this is that I added the following lines to PageNavi's core.php starting at line 32:

    if (preg_match("#/page/([1-9]|[1-9][0-9]|[1-9][0-9][0-9])?/#i", $_SERVER["REQUEST_URI"], $matches)) $paged = $matches[1];
    else $paged = 1;
    
    $posts_per_page = 4; // the number of posts I happen to have specified in my index.php loop

    Pagination mechanism works fine — root, /page/2, page/3, etc all display the posts they should.

    My problem is that from the plugin options under the Dashboard's Settings, I have to specify an integer of number of pages to show. Surely this should be derived by the total number of posts divided by the number of posts per page, and not some arbitrary hard-coded setting?

    Setting, for example, 4 currently gives links to pages with no content. Setting 1 displays only one page link when there are 2. Leaving the field blank defaults to 0, which in practice outputs

    « 2 1 » for page 1 and
    « 3 2 » for page 2.

    I'm wary of further hacking into the actual plugin core for what seems like bare-bones essential functionality for a pagination plugin.

    Am I missing a trick?

  2. Barney Carroll
    Member
    Posted 3 years ago #

    For anybody else falling over on this, I used the following hack — although I suspect it will become unwieldly with large numbers of pages. The code below is introduced after line 40 of /wp-content/plugins/wp-pagenavi/core.php (of version 2.74), and effectively overwrites values extrapolated otherwise taken from default or options.

    As far as this implementation goes, the number of posts per page needs hard-coding in as $posts_per_page (and you will need to follow the instructions in the blog post above if you are using Magazeen or another theme that doesn't play well with pagination by default):

    // (Hack to work with Magazeen{
    
    	$count_posts = wp_count_posts();
    	$published_posts = $count_posts->publish;
    
    	if (preg_match("#/page/([1-9]|[1-9][0-9]|[1-9][0-9][0-9])?/#i", $_SERVER["REQUEST_URI"], $matches)) $paged = $matches[1];
    	else $paged = 1;
    
    	$posts_per_page = 4;
    
    	$pages_to_show = ceil( $published_posts / $posts_per_page );
    	$total_pages = $pages_to_show;
    
    // })

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic