My problem is that I displayed content of 3 pages on index.php by using get_page(). The problem is that each of those pages have own page template

Template Name: another page template

which contains the loop with 3 posts in category.

This way I can see only content of page typed in WP editor but no trace of queried posts on pages.

How to display three posts from page template on index.php?

For displaying pages on index.php I'm using:

 $id = 1;
 $post = get_page($id);
 $content = apply_filters('the_content', $post->post_content);
 echo $content;

and pages have own template with the_loop:

<?php query_posts( array( 'category_name' => 'name', 'posts_per_page' => 3 ));
  while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
  • Please edit your question and clarify what you are doing, particularly this part, "and each of those pages have page template which contains the loop with 3 posts in category..." What page templates are you talking about? You can only really have one per page load. Are you trying to print 3 posts after the first loop, as your code seems to be trying to do, or 9 as your description implies?
    – s_ha_dum
    Commented Nov 29, 2012 at 1:26

1 Answer 1


You seem to be confusing the database object that is a post/page, and the rendered output that is a post/page. The data contained in the wp_posts and wp_post_meta tables define the post object. A template file defines the rendered output when a given database object is queried.

There are three types of queries: the default, main query for a given context, secondary queries defined by core (such as the Nav Menu), and custom queries, that are defined elsewhere (either by the Theme or by a Plugin).

The default, main query for a given context is never affected by custom queries, even though it can be modified, either by filtering pre_get_posts or by bludgeoning it with query_posts().

What happens when you call get_page() is that WordPress queries the post object associated with a given ID, not the template file that would be used to render that object in its normal context.

Long story, short: if you want to run in some other context the same custom queries in each of your three custom page templates, you'll need to execute the same code as you use on those three custom page templates.

(Also: please say you've not modified your Theme's index.php file for this purpose? By doing so, you'll completely break the template hierarchy for your Theme.)

The cleanest solution would be to split out the custom query code into template-part files, one for each custom page template; perhaps:

  • loop-three-latest-category-x.php
  • loop-three-latest-category-y.php
  • loop-three-latest-category-z.php

So, one of your custom page templates will look like so:

 * Template Name: another page template
 * Category X custom page template
 * Used to display the three latest posts in 
 * category x.


get_template_part( 'loop-three-latest-category-x' );


Then, to output all three custom queries on the site front page, create a template file called front-page.php, and call all three template-part files from above:

 * Front-page template
 * Used to render the site front page


get_template_part( 'loop-three-latest-category-x' );
get_template_part( 'loop-three-latest-category-y' );
get_template_part( 'loop-three-latest-category-z' );

  • Thank You Chip for deatiled answer and a solution! I'll definitely try Your way to solve my problem! :)
    – cachaito
    Commented Nov 29, 2012 at 14:20

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.