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.

I know this is a really newbie question, but I can't seem to get the loop to pull from the posts. all it is doing is pulling from the page itself.

I made a template and added the loop to it.

<?php
if( have_posts() ) {
  while( have_posts() ) {
    the_post();
    ?>
    <h2><?php the_title(); ?></h2>
    <?php
  }
}
?>

edit: here is all the code: http://pastebin.com/k2rDu53b

share|improve this question
    
If your code is itself running in the loop, you won't get expected results. Where is this snippet running? –  brandwaffle Dec 22 '11 at 20:16
    
right after the body, I thought that was the loop, the have_posts...? a little confused. here is all the code:pastebin.com/k2rDu53b –  user766607 Dec 22 '11 at 20:18
    
I meant if you were already in the loop, and then you had that code inside there...not what's happening anyway. What is the output you're seeing on the page? –  brandwaffle Dec 22 '11 at 21:43
    
Just tested this on my local WP install and it's showing me post titles as I'd expect. Is this in your theme folder and is it running as the active theme, or is it being loaded some other way? If it's a theme, do you have a style.css sheet with at least the basic info defined there? codex.wordpress.org/Theme_Development#Template_Files_List –  brandwaffle Dec 22 '11 at 21:57
    
Why all the header code in the template? Just call get_header() instead, if it needs to differ from the main header file, create another eg. header-two.php with the code and call that in the template instead, eg. get_header( 'two' ) –  t31os Dec 23 '11 at 12:25

1 Answer 1

up vote 4 down vote accepted

Because you're on a page, that's only going to display the query for that page. As such, you'd have to create a new query to bring in the posts you want. Replace your loop with this:

<?php
    $args = array(
        'post_type' => 'post'
    );

    $post_query = new WP_Query($args);
if($post_query->have_posts() ) {
  while($post_query->have_posts() ) {
    $post_query->the_post();
    ?>
    <h2><?php the_title(); ?></h2>
    <?php
  }
}
?>

Here some more information on the query: http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
    
this was it, thanks! –  user766607 Dec 27 '11 at 14:40

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.