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:
<?php
$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; ?>
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
andwp_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 withquery_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:
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: