I've created my own page template where I have a list of posts coming in directly below the content as so:

<?php the_content(); ?>            
<?php query_posts('category_name=vacancies'); ?>
<?php while (have_posts()) : the_post(); ?>         
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php // Reset Query
wp_reset_query(); ?>

This works fine on this page, however on the main index page my template is set up so it loads in the_content() four different pages. I want to include this within one of them e.g:

if query_posts() exists in page then load into the_content() on index page

Bit confusing, hope I'm making sense, any ideas?

  • 3
    You're totally bastardizing the order of output here. The call to the_content() should always be inside the Loop. And calls to query_posts() should only ever be used to modify the Primary Loop. If you need to query additional posts, you should use WP_Query() or get_posts(). That said: can you describe, in human - not code - terms, what you are trying to accomplish with each template file ( page.php and index.php)? Dec 19, 2011 at 12:58
  • Ok no problem, I know its a tad confusing lol. Template page.php (a template I created) - I want the content to show and a list of posts to show below it. That works with the code I wrote. Index page - load in the template page content and list of posts (without affecting other pages that load in).
    – gordymonty
    Dec 19, 2011 at 13:20
  • Basically I fixed up my template page with the code below. Now I want all the content and posts list to be displayed on index page, not just the content from the template.
    – gordymonty
    Dec 19, 2011 at 13:39
  • The page.php template file has a specific purpose in the WordPress Template Hierarchy. Generally speaking, you should not load the page.php template file inside the index.php template file - or inside any other template file, for that matter. Also, really still haven't explained, in human terms, what you're trying to accomplish. If you can do that, we can help you. Dec 19, 2011 at 13:57
  • Yeah I know I'm probably not explaining this well, sorry. I'm using a WordPress theme that loads content from 3 selected pages into the homepage. I want it so that for one of these pages (the one with the list) it doesn't just load in the content, it loads in the content and list. So how can I append the list to the content (if possible)?
    – gordymonty
    Dec 19, 2011 at 14:49

1 Answer 1


I don't know exactly how your code works BUT I do know this. If you only have one loop on the page-template you could use query_posts(); but if you have more than 1 loop it will screw up the code, even if you reset it. I would recommend you to use WP_Query like this:

// The Query
$the_query = new WP_Query('category_name=vacancies');
if($the_query) echo '<ul>';
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

if($the_query) echo '</ul>';
// Reset Post Data


  • On my page template I only have the one loop that's why it was working fine. If your way is the more correct way of doing it of course I'll use it! I wasn't aware of this...
    – gordymonty
    Dec 19, 2011 at 13:30
  • You could put if($the_query){the_content();} as well. So it is possible to detect query_posts / wp_query :)
    – jocken
    Dec 19, 2011 at 14:57

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.