0

I'm trying to limit the number of posts on the homepage but is_home() and is_frontpage() always seem to return true (even when I'm browsing /page/2/, /page/3/ and so on).

I've basically duplicated the twenty fifteen theme and trying to make my own based on that one. I deleted every function in functions.php (also removed any calls to those functions) and currently I only have this function in there:

add_action( 'pre_get_posts', 'limit_frontpage_posts' );

function limit_frontpage_posts( $query ) {

    if ( is_front_page() || is_home() ){
        $query->set( 'posts_per_page', 1 );
    }

  return $query;
}

Basically I want one post on the frontpage and on every other page whatever limit has been set in the admin panel. The code above is showing only one post on every page. Any idea's what's going wrong?

1
  • Under Settings -> Reading have you set a homepage or is the homepage also your blog ( which is default functionality )? It would also be best to test against the current query by prefixing your conditional statements with $query->is_front_page() and $query->is_main_query() so it doesn't run the filter on all queries on the front page.
    – Howdy_McGee
    Commented Sep 25, 2015 at 15:19

1 Answer 1

2

There's nothing wrong, the second page of your home page is still your home page. If you want to check if it's only the first page, check if ! is_paged().

That said, you can't just change posts per page and have it work properly, because when you're on the 2nd page and beyond, the post it starts on is calculated based on the current number of posts per page- WordPress doesn't know that your first page only had one post and the 2nd should start at post #2, it will just multiply posts per page by the page number, so posts will be skipped.

See this answer for an explanation and solution.

3
  • I was just about to post the same link to my answer :-) Commented Sep 25, 2015 at 15:22
  • I didn't realise each page was still considered the home page. Is it possible to make an entirely different page the home page, with a query that only selects the latest post? Or is the link you posted the way to go for this kind of thing?
    – wp902
    Commented Sep 25, 2015 at 15:34
  • I would implement the pagination fix on the linked answer. You can create a new question here with your attempted code if you are having trouble making it work.
    – Milo
    Commented Sep 25, 2015 at 16:28

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