I am designing a theme where the home page (i.e. is_home()
) should only display the latest blog post. I want to do something like $wp_query->update_option('posts_per_page', 1)
every time I am on the home page. All other pages that display posts (like archives) should just follow the user defined option.
This seems a bit unnecessary to do every single time since the option should just be set once, right? Is it possible within The Loop to just ignore the user-set option of posts_per_page
and force have_posts()
to just be set to one post?
In general, where should this kind of “set-it-once” stuff go? I don’t really think it should be a plug-in because it is theme specific. I also don’t want to mess with the user’s options which is why update_option
isn’t the best choice for this problem.
Alternate approach (if you want/need to keep this in functions.php and go via hooks instead of modifying template) would be something like this:
posts_per_page
should be set once the at settings reading panel, and acts as a general option unless defined else using query_posts() for example if you want your homepage to display only one then the place this code above your loop:I prefer use a function than use global variable, like this: