• I have a custom field “year”, for my posts. The amount of posts that are tagged with any single year varies.

    Currently in my loop I am showing 20 posts per page, in order of the tagged year. When the year changes, the year is inserted as a title, this continues for 20 posts (obviously). However the pagination does not correspond to the amount of posts for each year, so the years get broken up across pages. For example:

    1932: Post Post Post Post Post Post Post Post Post

    1933: Post Post Post Post Post Post Post

    1934: Post Post Post Post

    ——Next Page——-

    !934: Post Post Post Post Post Post and so on…

    This is a problem because I am loading the posts in an infinite scroll manner using ajax. When the next page of 20 posts are appended to the page I usually end up with one year being broken up, and having two titles, like so:

    1934: Post Post Post Post (these posts were on the current page)

    1934: Post Post Post Post Post Post (these posts are on the next “page” and have been appended)

    Is there any way that I can force the page count to change depending on how many posts are left for the current year, so one year never gets broken up. In a way force any “leftover” posts for that year to be included. Or perhaps there is another method to solve this problem?

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • You are going to have a tough time trying to bully ordinary pagination into this pattern, but this doable with some minimal custom code.

    Are you using a custom theme template to load this? If so, post the code that runs the query.

    Do you have so many posts per year that you cannot load an entire at once?

    Thread Starter beetrootman

    (@beetrootman)

    I don’t want to load all posts at once because there are about 250 and for each post there is an image, so it’s kind of a heavy load.

    Here is the code for the template:
    https://pastebin.com/ayv0qGku

    And the loop file:
    https://pastebin.com/RB3fZ4xf

    Bear in mind I am using this function to make pagination work for my custom post type:

    function paginate() {
        global $wp_query, $wp_rewrite;
        $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
        $pagination = array(
            'base' => @add_query_arg('page','%#%'),
            'format' => '',
            'total' => $wp_query->max_num_pages,
            'current' => $current,
            'show_all' => true,
            'type' => 'plain',
    		'prev_text' => __('←'),
    		'next_text' => __('→')
        );
        if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
        if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
        echo paginate_links($pagination);
    }

    I think I would pull one year at a time, not the range of years. Then when there are no more posts for the year, provide a link to the next year.

    Thread Starter beetrootman

    (@beetrootman)

    Ah yes, that’s a better idea, but I’m not sure how I would use ajax to add the posts, since at the moment it’s done automatically with the paginated pages. Any tips?

    Thanks

    Get your non-ajax query working and the AJAX query will be virtually the same. Just make sure to pass the right variables. Chances are slim that you will be able to use an “ajaxify” plugin right out of the box, but it shouldn’t be that hard either.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Dynamic number of posts per page’ is closed to new replies.