Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Can you show us all the code in template-blog.php? –  Charles Clarkson Sep 4 '13 at 20:33
    
You're looking at it-- the code starts with The Loop (as I posted above). There's obviously other HTML on the page to draw the blog-specific header and footer, etc, but none of that has to do with Wordpress. –  Eric Sep 4 '13 at 20:35
    
Where are you setting up the query arguments to tell the loop which posts to display on this page? –  Charles Clarkson Sep 4 '13 at 20:37
    
I see now, that was the problem :-) –  Eric Sep 4 '13 at 20:53

1 Answer 1

up vote 1 down vote accepted

before 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

    endwhile;
endif;
share|improve this answer
    
Thanks, exactly what I was looking for. –  Eric Sep 4 '13 at 20:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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