I am working on a one page WordPress site. It's the first time I have tried this. I am on the fifth version with various degrees of success. I am starting a new version and I deleted everything in the theme folder except the header.php index.php, functions.php, footer.php, page templates and styles.css

When I look in my database in the wp_postmeta, I can see that there are several pages in there that have a template assigned. I have seen this code before so I tried it for the first time to see what I could get. In my index.php file. I have the simple standard loop. you can see that right after the_post() I have tried the get_post_meta. My thought was that it would return all the names of the page templates. But it doesn't I actually get nothing. I tried a var_dump and a foreach loop to see what was in it. I get nada. I don't know why

As a secondary question still related, my query below does not return every post in the database. It is only returning 2 blog posts, ( there are only 2 ) and 8 posts out of a lot. It is not showing any pages that were made. Can you give me a hint as to why?

 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php $template = get_post_meta($post->ID,'_wp_page_template',false); ?>
    <?php var_dump($template); ?>
            <li id="page_<?php the_title(); ?>">

        <?php if(has_post_thumbnail()) {

                the_content('Read the rest of this entry »'); ?>
    <?php endwhile; ?>

    <?php else : ?>

    //Something that happens when a post isn’t found.
<?php endif; ?>
  • But index.php will display posts, not pages, right? What if you try in page.php? Or do a custom query? Search for Template Hierarchy here and in the Codex.
    – brasofilo
    Commented Jan 25, 2013 at 3:07
  • I checked the template hierarchy. Everything defaults back to the index.php file which is what I want. I guess my confusion came in when "The Loop" didn't supply all posts. There are a lot missing.
    – Jamie
    Commented Jan 25, 2013 at 3:21
  • Sorry, Jamie, I missed that part of the Q, my bad...
    – brasofilo
    Commented Jan 25, 2013 at 3:22

1 Answer 1


Only the page post type can have a template assigned (of the default post types), it's the custom template you set in the Page Attributes meta box. If you have a page.php template file, your index.php will only be used to display post post types, so those posts will never have a _wp_page_template meta key. As to why you don't get any pages with your index.php loop, only the post post type gets queried on home and archive pages. If you want to get all posts and pages on your home page, use pre_get_posts to modify the home page query:

function wpa83038_posts_and_pages_on_home( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page' ) );
add_action( 'pre_get_posts', 'wpa83038_posts_and_pages_on_home' );

I'm not sure how useful that would be though. Seems you'd want to do another custom query for pages and keep posts separate.

  • You had a good idea with the snipppet you posted. I forgot you could do that. I also forgot about the home issue. Thanks for pointing that out. That could be a big part of my issue. including a page, single and category.php is what I am trying now. So far so good.
    – Jamie
    Commented Jan 25, 2013 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.