Custom Posts Page Template
-
I am trying to display posts using a custom page tempalte. I set up my blogs page through Settings/Reading and defining the page there, and then defining the template in that page. The code in the template page is:
<?php /* Template Name: Blog page */ while(have_posts()) { // original main loop - page content the_post(); the_title(); // title of the page the_content(); // content of the page // etc... } // now we display list of our custom-post-type posts // first obtain pagination parametres $paged = 1; if(get_query_var('paged')) { $paged = get_query_var('paged'); } elseif(get_query_var('page')) { $paged = get_query_var('page'); } // query posts and replace the main query (page) with this one (so the pagination works) query_posts(array('post_type' => 'my_post_type', 'post_status' => 'publish', 'paged' => $paged)) // pagination next_posts_link(); previous_posts_link(); // loop while(have_posts()) { the_post(); the_title(); // your custom-post-type post's title the_content(); // // your custom-post-type post's content } wp_reset_query() // sets the main query (global $wp_query) to the original page query (it obtains it from global $wp_the_query variable) and resets the post data // So, now we can display the page-related content again (if we wish so) while(have_posts()) { // original main loop - page content the_post(); the_title(); // title of the page the_content(); // content of the page // etc... } ?> <?php get_header(); ?> <div id="container" class="clrfix"> <div class="contents"> <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?> <p>Date: <?php echo date("l, F d, Y"); ?> | <?php comments_number(); ?></p> <?php the_content(); ?> <h4>This is post #<?php the_ID(); ?> | Author: <?php the_author(); ?></h4> <p>Filed under: <?php the_category(','); ?> | <?php edit_post_link(); ?></p> <?php endwhile; ?> <p><?php previous_post_link(); ?> | <?php next_post_link(); ?></p> <?php else : ?> <p>Sorry, no posts matched your criteria.</p> <?php endif; ?> </div> <div class="sidebar"> <?php get_sidebar(); ?> </div> </div> <?php get_footer(); ?>
It is not working and displaying the regular index.php format. What am I doing wrong?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Custom Posts Page Template’ is closed to new replies.