最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - How do I create a template page to show 3 blog posts?

programmeradmin1浏览0评论

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?

Share Improve this question edited Sep 4, 2013 at 19:04 Eric asked Sep 4, 2013 at 18:56 EricEric 2824 silver badges20 bronze badges 4
  • Can you show us all the code in template-blog.php? – Charles Clarkson Commented Sep 4, 2013 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 Commented Sep 4, 2013 at 20:35
  • Where are you setting up the query arguments to tell the loop which posts to display on this page? – Charles Clarkson Commented Sep 4, 2013 at 20:37
  • I see now, that was the problem :-) – Eric Commented Sep 4, 2013 at 20:53
Add a comment  | 

1 Answer 1

Reset to default 1

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

        }
    }
发布评论

评论列表(0)

  1. 暂无评论