• Resolved odie2

    (@odie2)


    Hello,

    If answer for my question is somewhere, please link me solution. Do not know how search for it.

    Problem is I would limit main page posts limit to 5. But when on page 2 and following, have default limit to 10. However, when I write it with is_paged() 5 posts (from first page) are missing.
    Could I for example start from certain post number for use in is_paged()?

    Why I would do it? Well, I am displaying slider and partners only on front page when ! is_paged(), so there is too much content with 10 posts.

    Thanks in advance!
    Greetings

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    All WP pagination functions assume the posts_per_page value will be used for all pages and calculates query offsets accordingly. Thus you would need to manually specify the query offset to take into account the shorter first page. Doing so will break the WP pagination entirely, so you will need to take over the entire pagination calculation. This is not as dire as it sounds, a viable approach is outlined in Making Custom Queries using Offset and Pagination.

    Note that I currently have spotty Internet access and may not be able to respond to further questions for some time. I hope this alone is of some help.

    Thread Starter odie2

    (@odie2)

    Thank you so much!

    Did function to work in index.php
    If someone want use it in functions.php / plugins, rebuild it for your own.

    global $query_string;
    
    $front_limit = 5; -- change it for your own - limit on front page
    $my_query = 'ignore_sticky_posts=1'; -- if you want sticky posts on start, change "1" to "0"
    
    if ( !is_paged() ) :
    	$my_query .= '&showposts='.$front_limit;
    else :
    	function wpsites_exclude_latest_post($query) {
    		global $front_limit;
    		$page = get_query_var('paged');
    		$limit = get_option('posts_per_page');
    
    		$offset = ( - ( ($page - 2) * $limit + $front_limit ) );
    		$offset = strval($offset);
    
    		$query->set( 'offset', $offset );
    	}
    
    	add_action('pre_get_posts', 'wpsites_exclude_latest_post');
    endif;
    
    query_posts( $query_string . $my_query );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘front page query_posts limit posts only on first page’ is closed to new replies.