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.

Hi all I have this template page which get the page's post and then a list of posts from the category news.

<?php
/*
 * Template Name: News & Info
 */

get_header();
?>


<div id="cLeft">
    <?php
    if (function_exists('yoast_breadcrumb')) {
        yoast_breadcrumb('<p id="breadcrumb">', '</p>');
    }
    ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

            <div id="title">
                <h1><?php the_title(); ?></h1>
            </div>
            <div id="freetext">
                <?php the_content(); ?>
            </div>

        <?php endwhile;
    endif; ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
            <?php query_posts('cat=32'); ?>

            <div class="post">
                <h2><a href=""></a></h2>
                <div class="postDescr"></div>
            </div>
        <?php endwhile;
    endif; ?>

</div>

<div id="cRight">
    <h2>News &amp; Info:</h2>

    <ul id="submenu">
        <?php wp_list_pages('hide_empty=0&child_of=26&title_li=&sort_column=menu_order'); ?> 
    </ul>
</div>

<?php get_footer(); ?>

The second loop gets in infinite loop and I don't know what am I doing wrong.

Thanks in advance

share|improve this question
2  
maybe because you're calling query_posts() inside the loop? –  onetrickpony Jul 5 '11 at 7:55

2 Answers 2

up vote 3 down vote accepted

It was correctly pointed out what the issue is, but I also want to explain it so you have better idea of mechanics:

  • when you use query_posts() it writes down generated WP_Query object into global $wp_query variable;

  • have_posts() and the_post() functions are wrappers for methods of same name called using global $wp_query object;

  • so what you are doing in that code is keep overwriting $wp_query and keep asking it if there are posts to process. And there are always posts because as soon as you process post you kick query back to new clean state.

Also see When should you use WP_Query vs query_posts() vs get_posts()?

share|improve this answer

I agree with One Trick Pony.. what if you try this?

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
            <div id="title">
                <h1><?php the_title(); ?></h1>
            </div>
            <div id="freetext">
                <?php the_content(); ?>
            </div>
        <?php endwhile;
    endif; ?>

    <?php $the_query = new WP_Query('cat=32'); //or category_name=
    while ($the_query->have_posts()) : $the_query->the_post();?>
            <div class="post">
            </div>
        <?php endwhile;
        wp_reset_query(); ?>
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.