WordPress.org

Forums

WP-PageNavi
[resolved] Pagination page 2 for a certain number of posts breaks (3 posts)

  1. everyeurocounts
    Member
    Posted 1 year ago #

    Hi,

    I have just installed your plugin and it works great with a little fix for custom posts.

    But i am having 1 issue, on a page with 11 posts, if i set the posts per page to 10, the 2nd page doesnt work....the pages work if i set it to 5 posts per page though, so there are 3 pages.

    Any ideas?

    fix i use:

    http://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

    https://wordpress.org/plugins/wp-pagenavi/

  2. Lester Chan
    Member
    Plugin Author

    Posted 1 year ago #

  3. everyeurocounts
    Member
    Posted 1 year ago #

    ok i couldn't get that solution working with a post variable so i did it through session + Ajax instead, seems to be working ok for the moment:

    php:

    add_action('wp_ajax_postperpage', 'postperpage');
    add_action('wp_ajax_nopriv_postperpage', 'postperpage');
    
    function postperpage() {
    
        if ( !wp_verify_nonce( $_POST['nonce'], "postperpage_nonce")) {
          exit("unverified request");
       }   
    
    	if(!$_SESSION) {
    			session_start();
    		}
    
    	$postsperpage = sanitize_text_field($_POST['ppage']);
    	$_SESSION['postperpage'] = $postsperpage;
    
    	echo $postsperpage;
        exit();
    }
    
    function my_custom_posts_per_page( $query ) {
    
    		if(!$_SESSION) {
    			session_start();
    		}
    
    		if($_SESSION['postperpage']) {
    			$perpage= $_SESSION['postperpage'];
    		} else {
    			$perpage=20;
    		}
    
            $query->set( 'posts_per_page', $perpage);
    }
    
    add_filter('parse_query', 'my_custom_posts_per_page');

    jquery

    function sortPageSubmit(){
    
    		var postperpage = jQuery('#perpage').val();
    		console.log(postperpage);
    		jQuery.ajax({
    			type: 'POST',
    			url: "/wp-admin/admin-ajax.php",
    			data: {
    				action: 'postperpage',
    				ppage: postperpage,
    				nonce: "<?php echo $nonce ?>",
    			},
    			success: function (output) {
    
    				document.getElementById("sortbox").submit();
    
    			}
    
    		});
    	}

    sample html:

    <select id="perpage" name="perpage" onchange="sortPageSubmit()">
        		<?php echo '<option value="'.$perpage.'" selected="selected">'.$perpage.'</option>';
        		foreach ($postsperpage as $option) {
    				 echo '<option value="'.$option.'">'.$option.'</option>';
    			}?>
    
        	</select>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic