0

I'm trying to create the actual "blog" page that lists all the posts. It seems like such a basic thing, but i can't find out how to do it.

Do i create a page and assign it a special template? If i do that, wouldn't the loop just grab the page content? I have a loop in a file called loop.php, which i want to use to display the posts.

I tried doing something like this:

  • Create a page
  • Assign "blog" template to page
  • From the blog template, run get_posts(); which gets all posts.
  • Right after that, run setup_postdata()
  • Then just require('loop.php'); to list all posts.

Unfortunately, that just grabs the first post; not what i want.

Any suggestions?

EDIT

A dummy solution would be to do something like:

    $posts = get_posts();

    foreach($posts as $post) {
        setup_postdata($posts);
        require('loop.php');
    }
    wp_reset_postdata();

The only problem is adding pagination, which i will need.

10
  • There are about a dozen examples of exactly how to do this in the codex for get_posts().
    – totels
    Commented Nov 16, 2012 at 14:55
  • WordPress already handles this, under Settings > Reading you can designate a page to display posts which will be queried in the default query/loop.
    – Milo
    Commented Nov 16, 2012 at 14:57
  • I've read it all. The problem is that the actual loop is in another file, loop.php. Look at my example above, that should work.
    – qwerty
    Commented Nov 16, 2012 at 14:57
  • I'll look at that Milo!
    – qwerty
    Commented Nov 16, 2012 at 14:57
  • 2
    So if you don't use require('loop.php') it works? (Also, you may want to use the get_template_part() function to include template files.)
    – totels
    Commented Nov 16, 2012 at 15:01

1 Answer 1

1

Assuming you're only trying to display your blog posts index on a page other than the front page (i.e. because you've assigned a static page as your site front page), you shouldn't need to do anything special. Refer to the Codex for step-by-step instructions for creating a static front page and separate page for your blog posts index.

Some caveats:

  • Initially, don't create any custom page templates or other custom template files. Ensure everything is working with the fallback template files (page.php and index.php, as per the Template Hierarchy).
  • Note: if your current Theme includes a front-page.php template file, WordPress will use that specific template file to render the site front page.
  • Note: if your current Theme includes a home.php template file, WordPress will use that specific template file to render the blog posts index.
  • If you want to create a custom template file to render your blog posts index, use the home.php template file. Do not give this template file a Template: phpDoc header tag. You don't need to assign this template file to the static page assigned as page for posts. WordPress will use home.php automatically, if it exists.
  • You should have no need whatsoever to create any custom queries or custom query loops, either via get_posts() or WP_Query(). WordPress will use the main query loop to render the blog posts index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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