• Austin SEO
    • TastyPlacement in the Press
    • Meet the Team
  • Blog
  • Services
    • SEO Services
      • WordPress SEO Service
      • Magento SEO Services
      • Conversion Rate Optimization
      • SEO Audit Service
      • Why Google Certification Matters
    • PPC & Adwords
      • Adwords & PPC Management
      • Remarketing Services
      • Display Ad Management
      • Facebook Ad Management
      • Google Ad Grants Management for Non-Profits
      • Adwords App Install Ad Management
      • Product Listing Ad Management
    • Analytics & Data
      • Analytics and Monitoring
      • Google Tag Manager Experts
      • Data Studio Development & Consulting
    • Social Media & Local Marketing
      • Social Media Marketing
      • Local SEO
    • Web Development
      • Mobile Website Design
      • WordPress Development
  • Case Studies
    • SEO Case Studies
      • SEO Case Study: We Beat Lowes, Then We Beat Home Depot
      • SEO Case Study: Total Domination in Houston for Medical Provider
    • Analytics Case Studies
      • Case Study: Updated Design Yields 43% Increase in Conversion Rate
      • Case Study: PPC Optimization Yields Tripled Conversion Rate
    • Social Media Case Studies
      • Social Media Case Study: Hundreds of New Customers From Core Facebook Campaign
  • Portfolios
    • Display Ad Portfolio
    • Design Portfolio
    • Infographic Portfolio
    • SEO Testimonials
  • Contact
    • New Customers: Get to Know Us
    • Customer Service & Support
    • Referral Program
    • SEO Training Seminars
    • Job: Paid Search/PPC/Adwords Analyst
    • Job: Local Digital Marketing Specialist
    • Job: SEO/Marketing Junior Analyst
    • Privacy Policy & Terms of Use
  • Menu Menu

WordPress Tutorial: Display All Posts on a Page

March 13, 2012/36 Comments/in WordPress/by Michael David

How to Create an Interior /blog/ Page That Mimics a Traditional WordPress Front Page

We got hung up recently trying to create an interior blog page (i.e., www.agreatsite.com/blog)  for a client’s design. This problem is more common now with full-featured templates and frameworks that employ sliders and carousels on the front page that are triggered by a template’s index.php file.

First, Create a Custom WordPress Page Template

First, you’ll need to create a custom WordPress page template. All WordPress templates have a page.php file as part of the default template–we simply want to vary that file a little bit. Make a copy of your page.php file and name it page-blog.php.

Next, you need to enter a few lines of code at the very top of your new php file:

<?php
/*
Template Name: Blog
*/
?>

The code above is a naming tag–the template name, in this case “Blog” will be the name that appears in the template selection box at the WordPress page edit window, which we’ll screenshot below.

The Code

Now, you can’t simply run the regular WordPress loop on our custom interior page, we are going to use the WordPress template tag get_posts to query our WordPress database and grab our posts. The following code accomplishes this:

$myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>
  <div class="post-item">
    <div class="post-info">
      <h2 class="post-title">
      <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
      <?php the_title(); ?>
      </a>
      </h2>
      <p class="post-meta">Posted by <?php the_author(); ?></p>
    </div>
    <div class="post-content">
    <?php the_content(); ?>
    </div>
  </div>
<?php comments_template(); ?>
<?php endforeach; wp_reset_postdata(); ?>

For the purposes of illustration, a greatly simplified version of the preceding code, without any html markup, hrefs, author information, post date data, or comment section would be as follows:

$myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>
 <?php the_title(); ?>
 <?php the_content(); ?>
<?php endforeach; wp_reset_postdata(); ?>

How it Works

So what’s happening here? Well, the heart of the whole process is get_posts–this template tag queries the WP database and gets our posts.

Next, the foreach construct processes each post in turn–thus we’ll have all our posts on our blog page.  The setup_postdata WordPress function, well, sets up our data so it’ll display properly (otherwise the_content may not display the text of our posts. Finally, the wp_reset_postdata restores the $post global variable.

Once you’ve created the file, you’ll obviously want to upload it to your template (theme) directory.

Setting Your Blog Page

Your next step is to simply set up your blog page within the WordPress dashboard. From the WP dashboard, go to Pages, then Add New and create a page with a title “Blog” (or whatever is suitable). Remember the custom template we created above under the heading First, Create a Custom WordPress Page Template? You should now see your custom template name appear under the “Template” pull-down in the Page Edit screen, as indicated in the pic below by the green arrow.

Set the blog page by selecting the “blog” template

You don’t need to put any text in the text edit window, you just need a title–you won’t be displaying any page text here, you’ll be bypassing the specific text of this post and grabbing posts from the database.

Some Background on Why This Was Needed

Incidentally, framework and template designers that hijack WordPress’ index.php file to display a homepage slider, while requiring WordPress’ reading settings to be set to “Your latest posts” as shown in the screenshot below are doing a disservice to users (hence mandating this tutorial). The sounder practice is to code sliders and homepage features into a custom WordPress template.

Tags: get_post, tutorial, WordPress
Share this entry
  • Share on Facebook
  • Share on Twitter
You might also like
Are Site-wide H1 Tags in WordPress Good or Bad?
WordPress Stripping iFrame Elements? Here’s the Fix.
From the Wordpress SEO book Should You Disallow Old Link Structures With Robots.TXT?
How To Delete All WordPress Pending Posts
Setting Up a Stripe.com Single Payment Page for WordPress
How to Add a Sidebar to Your WordPress Theme
36 replies
  1. Bob Thomas says:
    September 30, 2012 at 10:47 am

    Hey Michael. This is exactly what I want to do on my site but it is not working for me. I am getting this error: “Parse error: syntax error, unexpected T_ENDFOREACH in /home/a7674789/public_html/wp-content/themes/Recording and Mixing Studio Site 4L/page-blog.php on line 25”

    I have followed everything you described here and am wondering if there is something I need to change in the code? I am by no means comfortable with php but am not afraid to get my hands dirty.

    Thanks

    Reply
    • Michael David says:
      October 22, 2012 at 3:39 pm

      It’s too hard to tell from your error message, and we don’t want to paste code here, but the error means you have a foreach statement that begins but is not terminated properly. This can be just one bracket causing this.

      Reply
    • Casper says:
      January 4, 2013 at 1:46 pm

      I have the same error what did u do to fix it ?

      Reply
    • Kyle says:
      February 12, 2013 at 10:07 pm

      Im sure you have worked it out by now but for future visitors. Put a <?php before the start of the code.

      Reply
  2. nan says:
    October 14, 2012 at 8:47 pm

    THANK-YOU SO MUCH!!! as you stated, my theme had a built in homepage template which i didn’t realize until i had almost finished building the static pages and tried to send my blog entries to a static page – it wouldn’t work and i was going crazy. now i can tear myself away from the computer finally and go to sleep!

    Reply
    • Michael David says:
      October 19, 2012 at 8:58 pm

      …and our work here is done

      Reply
  3. Dz says:
    October 22, 2012 at 1:46 pm

    How do you restrict the code from showing some some of the posts?
    Thanks

    Reply
    • Michael David says:
      October 22, 2012 at 3:34 pm

      WordPress handles this easily, you can designate posts by category, or include/exclude particular posts by ID number by chaning the “get_posts” portion of the code shown above. You’ll want to see the WordPress Codex here: http://codex.wordpress.org/Template_Tags/get_posts

      Reply
  4. krissybwuteva says:
    November 29, 2012 at 10:10 pm

    Thank you soooo much! This worked great so I can manipulate my front page to how I want it. ! Although I didn’t realize for a while I had to add the <?php in the beginning of the code you gave us, I finally got it and it's working great.. One question: is there a way to only show excerpts instead of full articles on my new front page?

    Reply
    • Michael David says:
      December 8, 2012 at 7:25 am

      I think you can, actually. Look for the line of code in our tutorial that has this: “the_content();” …That command simply pulls the content of a post. If you replace it with “the_excerpt();” then you should pull the excerpt rather than the full content of the post. But I am writing this from memory so it might need some tweaking. And then, “the_excerpt” command also has some parameters so you can show different numbers of words/characters in the excerpt. As always, when coding in WP, refer to the WordPress Codex.

      Reply
  5. Branko says:
    January 5, 2013 at 3:38 pm

    Thank you for this great tutorial!

    Reply
  6. bessy says:
    January 5, 2013 at 6:30 pm

    thnx for sharing ,i won creat page for maps for wp geo maps for all post

    Reply
  7. brad says:
    January 8, 2013 at 8:31 am

    Hey Micheal,
    Thanks so much for your post, this saved me tons of time.

    Reply
    • Michael David says:
      January 13, 2013 at 11:11 pm

      you’re welcome, sir…

      Reply
  8. marta says:
    January 16, 2013 at 2:20 pm

    Hello,

    I’m using Neptune wordpress free theme, and I’m having some problems (I’ve wrote to the developer but he didn’t answered me yet, so I write to you looking for some help or advices).
    I would like to know how to activate my sort items (that correspond with my portfolio categories) “All//Gráficas//Identidad//Ilustración//Vídeo”
    in this website :
    http://www.martatomas.com/

    And I’ve just found a new problem: in my home page, when I try to go to its page nº2, occurs a 404 error.

    Thanks a lot for your help,

    Reply
  9. Dave says:
    February 11, 2013 at 12:46 pm

    I am using this to try and accomplish a static page showing recent post, but am having an error pop.

    $myposts = get_posts('');
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>

    <a href="" title="">

    Posted by

    The only code on my page is what you show here. Line 25 is:

    Any suggestions?

    Reply
  10. Dave says:
    February 11, 2013 at 12:51 pm

    so the code attribute isn’t working..

    Regardless, I have any error on line 25 which is the last line of your example code php end of reach – http://d.pxlrmpg.com/NpA5.jpg

    Reply
  11. Jesicca half says:
    February 13, 2013 at 3:23 am

    Thank you for this great tutorial!

    Reply
  12. helios says:
    March 5, 2013 at 3:09 am

    Hi Michael

    how do i get the previous and next post links after say I only show 5 posts in a page. I tried using the code used for my theme (Im using Roots theme) for the next and prev posts but its not working. Any pointers?

    Reply
  13. Rishi says:
    March 17, 2013 at 2:46 am

    Great work, you saved my day. Especially the simplified version of the preceding code :-)

    Keep posting these kind of quick tutorials!

    Reply
  14. Divya says:
    April 3, 2013 at 2:17 am

    Thanks a lot for the tutorial… One question…I want “Recent Blogs, Recent Comments, Archive etc” information in the sidebar on the right hand side of the blog page. Currently it is being displayed at the bottom(after all posts). How can this be achieved?

    Reply
  15. Lars says:
    May 8, 2013 at 3:15 pm

    Thanks, totally helpful!

    Reply
  16. Laura @ hip pressure cooking says:
    May 16, 2013 at 4:59 am

    Ok,, I was able to get pretty far following the article and the comments… I did replace the_content with the_excerpt except now I no longer have the first image of the post.

    How can I display the first image AND the excerpt?

    http://www.hippressurecooking.com/recent-articles/

    Thanks,

    L

    Reply
  17. Ryan Karpeles says:
    June 27, 2013 at 12:47 pm

    Dude, THANK YOU!!!! This is exactly what I needed, and nobody else had this code anywhere. Thank you again!

    Blessings,
    Ryan

    Reply
  18. Nizam Kazi says:
    July 17, 2013 at 4:08 am

    This is code is working, but only 5 posts are being displayed on my page, do you have any idea why?. Also try to add option of pagination.

    Reply
  19. Dev Abhi says:
    August 15, 2013 at 12:09 am

    its working but instead of post titles the blog page title is getting displayed with all blogs

    Reply
  20. canhead says:
    October 10, 2013 at 3:50 am

    Great blog very helpful fella
    But how can you display them as a snippet of text with a read more which takes you through to the full article?

    Reply
  21. Hasan says:
    October 11, 2013 at 11:23 am

    Been looking for this info since 2 hours, working like a charm for my blog :)

    thanks Michael.

    Reply
  22. Matt says:
    October 18, 2013 at 8:38 am

    Great post. Hadn’t run into this scenario in a long while (home.php displaying posts, with additional blog archive page). This post did the trick! :) Cheers.

    Reply
  23. James Paterson says:
    January 22, 2014 at 12:04 pm

    <3<3, been looking for this and you saved my bacon.

    Reply
  24. hugh says:
    May 5, 2016 at 7:41 am

    Thanks it works fine except I ‘m going crazy because the blog pages is not respecting the styles
    here’s my code.


    Reply
  25. ANAND says:
    September 29, 2017 at 8:55 am

    Are there any plugins to show all posts in a single page. Not like individual post.

    Reply
  26. Christian says:
    December 16, 2018 at 1:52 pm

    Love it! I created an option to display all my blog posts on one page, export the content with images and will convert it with prince into a PDF-file for a book print. First step done!

    Reply
  27. alejandro says:
    January 3, 2020 at 4:28 pm

    thank you, this worked

    Reply
  28. Chevelle says:
    March 6, 2021 at 1:37 pm

    If I ve already been posting my blog posts to my home page and want to create a blog page how do I transfer the blog posts over? Will simply creating a new page called blog do that automatically?

    Reply
  29. Shawnette says:
    March 17, 2021 at 6:20 pm

    I tried method #1 and created an archive page, but it only displayed five posts on the page. How do I get it to display all of them?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tutorials & Case Studies

  • Analytics
  • Case Studies
  • Infographics
  • Internet Marketing
  • Local Maps and Local Listings
  • Magento
  • Mobile SEO
  • Our Book: SEO for Wordpress
  • PPC
  • Programming & PHP
  • SEO
  • SEO Power Tools
  • SEO Resources
  • Social Media Marketing
  • Web Design
  • WordPress

Our Most Recent Tutorials & Case Studies

  • Determining how and when Google Analytics 4 collects website event data
  • Infographic: Fonts & Colors That Drive the World’s Top Brands
  • Research Shows a 23% Divergence Between UA and GA4
  • How to Track Google Analytics Conversions on BuilderTrend’s iFrame Form
  • Test Results: How to Stop Google Re-Writing Your Title Tags in the SERPs

Search

Archives & Tutorial Categories

  • Analytics
  • Case Studies
  • Infographics
  • Internet Marketing
  • Local Maps and Local Listings
  • Magento
  • Mobile SEO
  • Our Book: SEO for Wordpress
  • PPC
  • Programming & PHP
  • SEO
  • SEO Power Tools
  • SEO Resources
  • Social Media Marketing
  • Web Design
  • WordPress

Austin SEO Company, TastyPlacement

Click Here to Explore a Free Consultation

Our Most Popular Services

  • Austin SEO [Home]
  • WordPress SEO Service
  • PPC Management
  • Social Media Marketing
  • Analytics and Monitoring
  • Remarketing Experts
  • Conversion Rate Optimization

Let’s Talk: How to Get in Touch With Us

TastyPlacement
4150 Freidrich Ln Ste C
Austin, TX 78744
Tel: (512) 535-2492


Get Directions or Read Our Awesome Reviews on Google Maps
© Copyright - TastyPlacement Inc.
  • Twitter
  • Facebook
Pictures From Pubcon Paradise 2012 How to Promote an Infographic
Scroll to top