• I have a loop running on my homepage that requests 18 posts and splits them between two div blocks to create a 3 column layout. The loop was not written by myself as I do not have a great understanding of php and I needed it preserve the original query once the site was paginated.

    I have tried to apply this same loop with a tag specific query to a page. I am using exec-php to allow the page to run the php. The result is a perfect display of posts in 2 div blocks but a repeated query below.

    I believe that the problem lies in that I have in effect created a nested loop (one loop inside another) e.g.

    <– Page template loop starts –>

    <– Custom loop starts from inside page loop –>

    <– Custom loop should end –>

    <– Page template loop ends–>

    I have tried many variations, from creating a custom template for the page with the loop in, closing the loop early and so on.

    If anyone could advise on the best way to solve this, it may be that a custom template page is required, spliced with bits of a page template.

    I have copied and pasted what is currently sat inside the page:

    <div id="educationposts">
    	<?php
    
    			$i = 0;
    		    query_posts($query_string.'tag=locws-schools&posts_per_page=18');
    		    while (have_posts()) : the_post();
    
    		?>
    
    	      <?php if ($i == 0) { echo '<div id="content" class="firstcolumn">'; } ?>
    		  <?php if ($i == 10) { echo '<div id="content" class="secondcolumn">'; } ?>
    
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    					<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2><img src="<?php bloginfo('stylesheet_directory'); ?>/images/line.gif" class="line">
    
    					<div class="entry">
    						<?php the_content('More'); ?>
    					</div>
    
    					<p class="postmetadata">
    							<?php
    							if(get_the_tag_list()) {
    							 get_the_tag_list('<ul><li>','</li><li>','</li></ul>');
    							}
    							?>
    
    						Category: <?php the_category(', ') ?>
    					</p>
    
    				<br />
    				<div class="timeblock">
    				<small>
    				<?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    				</div>
    				</div>
    
              <?php if ($i == 9) { echo '</div>'; } ?>
    
    		<?php $i++; endwhile; ?>
    
    </div>
    </div>

    Please remember, this is inside a page so there will be php wrapped around this from the default page template in the theme, and here it is…

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content" class="narrowcolumn">
    
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post2" id="post-<?php the_ID(); ?>">
    		<h2><?php the_title(); ?></h2>
    			<div class="entry">
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    			</div>
    		</div>
    		<?php endwhile; endif; ?>
    	<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Thanks for your time

Viewing 2 replies - 1 through 2 (of 2 total)
  • Using the WordPress Default theme and the wp-content/themes/default/page.php, right after this code:

    <h2><?php the_title(); ?></h2>

    I put this code and it seems to work okay:

    <?php
    $parent_id = $posts[0]->ID;
    $args=array(
      'post_parent' => $parent_id,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
       the_content('More');
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thread Starter lawrencebrown

    (@lawrencebrown)

    Thanks for your reply Michael, I marked up /page.php with no luck, it still looped twice.

    I tried once with just adding your code and another time with removing all of the loop from the /page.php and just leaving your new code

    Both times it looped twice.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Loop looping twice, only inside pages’ is closed to new replies.