Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>
share|improve this question
    
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 Nov 29 '12 at 1:26
add comment

1 Answer

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_header();

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

get_footer();

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:

<?php
/**
 * Front-page template
 * 
 * Used to render the site front page
 */

get_header();

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

get_footer();
share|improve this answer
    
Thank You Chip for deatiled answer and a solution! I'll definitely try Your way to solve my problem! :) –  cachaito Nov 29 '12 at 14:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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