I have a blog theme that I created which shows one post per page. To do this I simply set up single.php
the way I wanted, and I have some code that links the 'blog' item in the nav to the latest blog post, and it all works fine. Yay.
Now the client wants to see 3 posts per page, so I created a new template-blog.php
file, and then created a page a /blog
to display three posts in a row.
My template-blog.php
file starts with The Loop:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
Yet it never actually gets any posts, so the page at /blog
displays a blank blog post.
How can I get Wordpress to give this template 3 blog posts?
I tried just upping the # of blog posts per page in Settings/Reading to 3, but that did nothing. Then I set my "Posts" page (also in Settings/Reading) to the Blog template, and now it uses the content.php
file to display one blog post (poorly) and completely ignores my template-blog.php
file. I didn't even know about the content.php
file before this?!
I think the answer is that I probably shouldn't be using a template-blog.php
file at all, and instead I should be changing some other file(s) to display three posts at once. I can work on editing content.php
so it displays one single post correctly, but the rest of the page design still needs to be there (like the special header I have for the blog, etc, currently in template-blog.php
). How?
I have a blog theme that I created which shows one post per page. To do this I simply set up single.php
the way I wanted, and I have some code that links the 'blog' item in the nav to the latest blog post, and it all works fine. Yay.
Now the client wants to see 3 posts per page, so I created a new template-blog.php
file, and then created a page a /blog
to display three posts in a row.
My template-blog.php
file starts with The Loop:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
Yet it never actually gets any posts, so the page at /blog
displays a blank blog post.
How can I get Wordpress to give this template 3 blog posts?
I tried just upping the # of blog posts per page in Settings/Reading to 3, but that did nothing. Then I set my "Posts" page (also in Settings/Reading) to the Blog template, and now it uses the content.php
file to display one blog post (poorly) and completely ignores my template-blog.php
file. I didn't even know about the content.php
file before this?!
I think the answer is that I probably shouldn't be using a template-blog.php
file at all, and instead I should be changing some other file(s) to display three posts at once. I can work on editing content.php
so it displays one single post correctly, but the rest of the page design still needs to be there (like the special header I have for the blog, etc, currently in template-blog.php
). How?
1 Answer
Reset to default 1before start your posts loop in a custom page template you need to get posts from database by using wp_query()
or get_posts()
do some thing like this.
$args = array(
'posts_per_page' => 3
);
$my_query = new WP_Query($args);
// now start your loop
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
// print post data, title, content .etc
}
}
template-blog.php
? – Charles Clarkson Commented Sep 4, 2013 at 20:33