How to include posts within a page using WordPress

16.08.2008 0

Hey everyone. Thought I’d share a solution to a problem I just solved.

I was creating a portfolio of case studies for a client and I want to use the “posts” within WordPress to list the case studies. However, the main menu uses the ‘wp_list_pages’ function, so I cannot interject a “portfolio” category link where I want that link placed within the menu, since it is auto-generated.

This means I could hard-code the portfolio category link at the beginning or end of the menu - but I need it somewhere in the middle, so I devised this workaround.

I have found the best way to do this is through the creation of a page template. This offers you the greatest flexibility, and will also help you learn a bit more about WordPress.

OK - enough blabbing, let’s get started.

  1. First thing to do is look in your theme folder for a file named page.php - make a copy of it and rename it to an appropriate name - we’ll use Portfolio.php for this example.
  2. Next, open the new file (portfolio.php) and copy and paste this into your file at the very top (change the name ‘Portfolio’ in the code to the name of your choice - this is the actual name of the template that will be listed when selecting the template from within the Write/Manage Page editor:<?php
    /*
    Template Name: Portfolio
    */
    ?>
  3. We will be pulling all posts from within a single category into this page. Create your new category now, and take note of the category’s ID. If your category already exists, just get its ID.

    Note
    : To find the ID, go to Manage >> Categories and hover your mouse over the Category Name. You will find the ID as the last parameter of the URL in the browsers status bar (bottom-left corner)
  4. Ok, back within the Portfolio.php, we need to replace the default code with new code that will import the posts from the category we’ve created / selected. Here’s how:
  5. Replace the loop (code varies so I have just included the beginning and ending tags - replace / remove everything between the tags and the tags themselves: <?php while(have_posts()) : the_post(); ?>
    ………
    <?php endwhile; ?>

    with this below - where ‘category=[the ID of your category you noted in step 3]‘:

    <?php
    $lastposts = get_posts('category=3');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>
    <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
    <?php endforeach; ?>

  6. Save the template.
  7. Create / Select the page you want to import the posts within - you should be in the page editor
  8. From within the page editor, under the Page Template dropdown, select ‘Portfolio’
  9. Publish / Save the page.
  10. Create posts under your selected category, if they are nor already present.
  11. Go to the front-end of your website and locate your new page. You should see the page displaying the posts you have created from within your selected category.
  12. As for placing the page’s link in the menu - just set your page order for the page.

That’s it. Pretty cool, huh?

WordPress templates are easy to create and offer you tons of flexibility.

Obviously, you can place any code you like in a template. The above is just an example of including the posts from a specific category. There are more option at WordPress.org.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • Blogosphere News
  • E-mail this story to a friend!
  • Fark
  • Furl
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • YahooMyWeb

No comments

Leave a reply