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'm developing a wordpress theme that uses a static page as front page.

My wordpress is configured to use "Home" page as its front_page and "Blog" page to show the blog posts.

When i try to use the loop on my "Blog" page to list the posts i get a odd behavior, the loop runs fine but any method to get the post information returns an empty string.

Sample code:

if (have_posts()) {
  while(have_posts()) {
    the_post();
    $posts[$index] = array(
                'title' => get_the_title(),
                'permalink' => get_permalink(get_the_ID()),
                'content' => get_the_content(),
                'day' => get_the_date('j'),
                'month' => get_the_date('M')
                );
    $index++;

  }
}

If i have 5 posts that yould return me an array with 5 elements, all of them with empty strings as the values.

If i add query_posts('posts_per_page=5');. it works, but that shouldn't be needed.

What am i missing?

You can check the complete source code on github: https://github.com/albertein/coltsintheme

Thank you

UPDATE: Even if i avoid using a static page as front page (delete front-page.php and configure wp con Settings>Reaging) I'm getting the same results which seems weird to me, it seems I'm missing something.

share|improve this question
    
Out of curiosity, is there any reason that you're adding another abstraction layer on top of the Template Hierarchy? Such unnecessary complexity often leads to these sorts of issues. Is there any reason you can't rename blog.php as home.php, and rename main.php as front-page.php, and just let WordPress handle the template laoding? (Also: your index.php won't work outside of the Front Page and the Blog Posts Index. Is this be design? –  Chip Bennett Mar 15 '12 at 22:21
    
You are also using short-tags in your code <? should be <?php –  Jeremy Jared Mar 15 '12 at 23:43
    
@ChipBennett Even if i use strictly the template hierarchy (Moving blog to home and main to front-page) I'm getting the same behavior. index.php doesn't do anything else because i was waiting to solve this issue to avoid to construct a custom query on every page to make it work. –  albertein Mar 16 '12 at 1:15
    
@ChipBennett i uploaded the code that follows wordpress template hierarchy to github, but still doesn't work. –  albertein Mar 16 '12 at 1:45

1 Answer 1

up vote 0 down vote accepted

Just before the snippet i posted i was doing something like $posts = array();, it seems that wordpress keeps the collection of the posts to be used on the loop in a variable of the same name, so i was meesing with the data, just changed $posts for $items and now works fine.

share|improve this answer

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.