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 can't get the loop to work as it's supposed to, without using the not-so-recommended:

<?php query_posts('post_type=post&post_status=publish&paged='. get_query_var('paged')); ?>

I'm using a custom page template named "template-blog.php" with this content: http://pastebin.com/raw.php?i=9W3akEcX

And here's the "content.php" file: http://pastebin.com/raw.php?i=EH6xhzLQ

Using that loop, I get only one post, which is actually a page to which I've assigned the "template-blog.php" template, in the form of a post - only the title, since I haven't added any content to it when I created it the dashboard. Is there anything wrong with my code above? Thanks.

share|improve this question
1  
please check and repair the pastebin links. –  Michael Aug 30 '13 at 16:14
2  
Please do not use pastebin links. Paste the code here. –  vancoder Aug 30 '13 at 16:15
    
Is it against the rules or why? –  Daniel Sarmas Aug 30 '13 at 17:31
    
Why do you want to make it harder for people to help you? And in a year's time, when those pastebin links no longer work, what good will your question be to other people? –  vancoder Aug 30 '13 at 22:20

2 Answers 2

The problem is not that the loop is not working. The problem is your code is not executing the loop the way you want it to.

PHP does not do what you mean. It does exactly what you tell it to do. Like most computer languages, it is just an extremely fast and incredibly dumb list processor.

When you removed the query_posts() call you needed to replace it with something else that tells PHP what you want. PHP doesn't just "know" anything.

Here is one solution. At the bottom of your template file, replace this:

<!-- START footer -->
<?php get_footer(); ?>

With this:

<!-- START footer -->
<?php

get_footer();

add_action( 'pre_get_posts', 'wpse_112282_change_query' );

function wpse_112282_change_query( $query ) {

    if ( $query->is_main_query() ) {

        $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

        $query->set( 'post_type', 'post' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'paged', $paged );

    }
}

This uses one recommended way to get the value of the $paged and follows the rest of the query you used in the query_posts() call. The first argument in query_set() is the value in your query that is to the right of the equal sign (=).

For example, post_status=publish becomes:

$query->set( 'post_status', 'publish' );

Read more about using the 'pre_get_posts' action.

share|improve this answer
    
Hey Charles, thanks for your response, but it didn't work... pastebin.com/ak6RGjTv Why can the twentytwelve theme work fine with only using this thing? <?php if(have_posts()) : while(have_posts()) : the_post(); ?> –  Daniel Sarmas Aug 30 '13 at 17:34
    
Sorry. Move the add_action line to near the top of the file. –  Charles Clarkson Aug 30 '13 at 23:54
    
It still didn't work... –  Daniel Sarmas Aug 31 '13 at 8:39

I finally managed to solve this problem ( without having to use <?php query_posts('post_type=post&post_status=publish&paged='. get_query_var('paged')); ?> ) by renaming the "template-blog.php" into "home.php", and after that I set the template of the page where I wanted my post to be displayed (in my case, "Blog") to "Default Template" and then I went to Settings -> Reading and selected "Blog" as my posts page.

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.