WordPress.org

Ready to get started?Download WordPress

Forums

Custom Posts Page Template (4 posts)

  1. ealatrebi
    Member
    Posted 1 year ago #

    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?

  2. I don't understand why you would want to do this? If you've set your settings > reading to a static page and then a page for your posts, then your page.php will be used for your homepage and the index.php used for your blog.

    If you want to make the index.php look different, just copy the code you've inserted in your custom template in your index.php.

  3. ealatrebi
    Member
    Posted 1 year ago #

    Excellent, thank you! I didn't realize the hierarchy of things, I didn't have a page.php file to begin with, renamed/saved index.php to that and applied the custom posts page to index.php, works fine now. Thanks again :)

  4. I like to use front-page.php for my home page. If I'm making a unique looking homepage, I find that this template works really well for static content and then I use the index.php for the blog.

    But if what you have works, then great. Glad I could help.

Topic Closed

This topic has been closed to new replies.

About this Topic