Themeforest

How To Add Blog Posts To A Wordpress Page

By default pages in Wordpress websites will not display blog posts. However, there is a quick an easy way to edit the code of your theme page to handle this.

1. Wordpress Theme Page Code

First create a new page for your theme. Then add the following code to the web page in the area where you normally display the main content:

<?php
global $more;
$more = 0;

  $post_per_page = 10; // -1 shows all posts
  $args=array('category_name' => 'Latest News', 'posts_per_page' => $post_per_page);
?>

<?php query_posts($args); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div class="post" id="post-<?php the_ID(); ?>">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
            <?php the_content('read more ...'); ?>
        </div>

    <?php endwhile; ?>
<?php endif; ?>

The above code allows you to select posts from a specific categories and display the excerpts on the page. In this example we select our “Latest News” category – change the ‘category_name’ => ‘Latest News’ to suit your requirements.

You can also control the number of posts displayed by changing the “$post_per_page” parameter.

2. How To Use In Your Page

Very simply – upload your new template file to your themes folder, create a new page in your Wordpress admin and assign the new template file to this page.

There you have it – a custom page on your Wordpress website showing latest posts. To add more pages just repeat the process by creating a new theme page file and change category name parameter.

7 Comments

  • Great post. Haven’t tried this yet but I intend to soon. Does this method display the entire post? What if I want to only show the post headline or headline and an excerpt? And what if I’d like to include a lead in to the post, such as, “Check out my latet post…”

    thanks

    • Hi,

      If you want to only show the headline you can remove the code that displays the post content and only show the post title:

      <div class="post" id="post-<?php the_ID(); ?>">
          <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
      </div>
      

      Additional text can be added directly to the code and it will appear in each post entry

  • Useful post! Thanks for sharing.

  • I’m stuck do I add the code into the page where I want the blog posts or do I add it into the themes?

    • Hi Andy,

      You add it to the theme file

  • what do you mean “By default pages in Wordpress websites will not display blog posts”..? Isn’t that what WP is mostly about??

    • The tutorial refers to a “Wordpress page”, which has a specific meaning. Pages in Wordpress are for non-blog post content that usually isn’t time-dependent – examples of pages you could add to your website are – about us, company profile, contact details, etc. These are created using the “Page” options in the Wordpress admin menu.

      Pages would generally be accessed via your main navigation or sub-navigation

Leave a comment

To add code to your comments wrap the code text in [text][/text] tags