Reputation: 41
On my website, I want a landing/homepage and a blog page. I went to Settings > Reading > Your Homepage displays "static page" and chose my homepage & blogs page.
My theme uses a grid layout for the posts. On my homepage, I want to show 6 posts and then a "more posts" link to the rest of the posts on /blog.
I want the homepage to display the posts like my theme was set to. Any widget or plugin I use does not display the posts like my theme does, so I was thinking of editing the
<?php while (have_posts()) : the_post(); ?>
code to only show the latest 6 posts. and add it to the "homepage" page. Is this possible?
Below is a picture of how my theme displays posts.
Upvotes: 1
Views: 37
Reputation: 7843
In short, you need to set the main WP_Query at frontpage to have the "post_per_page" parameter set to 6. You may use the pre_get_post hook to alter the main query. WP_Query provides an is_front_page method for you to check if the query is of the front page.
In your theme's functions.php, add these lines:
add_action('pre_get_posts','alter_query');
function alter_query(WP_Query $query) {
if ($query->is_main_query() && $query->is_front_page()) {
// Only alter the frontpage main query
$query->set('post_per_page', 6);
}
}
The have_posts(), the_post() functions all works with the main WP_Query object. Hence all should be working according to the new query setup.
Upvotes: 0