Pages

Wednesday, September 26, 2012

How to display posts in related pages?

First of all we need to create a category in posts. For example i want to display events posts in events page. So i have to create a events category in Posts Category. Then add all posts for events. Now create a page in the themes folder. And copy the below code in that page.

<?php
/**
 * Template Name: Posts Display
 *
 * Selectable from a dropdown menu on the edit page screen.
 */
 get_header();
?>



        <div id="primary">
            <div id="content" role="main">

        <?php
        if (is_page()) {
        $cat=get_cat_ID($post->post_title); //use page title to get a category ID
        $posts = get_posts ("cat=$cat&showposts=5");
        if ($posts) {
        foreach ($posts as $post):
        setup_postdata($post); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <?php the_content(); ?>
        <?php endforeach;
        }
        }
        ?>
            </div><!-- #content -->
        </div><!-- #primary -->

<?php get_footer(); ?>

 So when your creating a new page means in this we are taking as events page take template as "Posts Display". and save.
Note: Don't forgot this will work when the posts category name page which we creating should be same.

No comments:

Post a Comment