WordPress.org

Forums

[resolved] issues with multiple queries on home page (6 posts)

  1. phendyr
    Member
    Posted 2 years ago #

    I greatly appreciate anyone's time to respond and help point me in the right direction.

    I'm trying to set up two queries on my wordpress homepage, the first to display the most recent post title and excerpt, the 2nd to display the 3 most recent posts.... Unfortunately it's pulling the same data for both loops. I do have the reset_query function in place on the first one, but still not working. Below is my code, I may very well be using outdated practices, I've been trying to piece this together from bits and pieces on the web....thanks for taking a look and helping me figure this out.

    <div id="left1">
       <?php if ( is_home() ) { query_posts( 'posts_per_page=1' ); } ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
       <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
       <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    
       <h6><?php include (TEMPLATEPATH . '/inc/meta.php' ); ?></h6>
    
       <div class="entry">
       <?php the_excerpt(); ?>
       <h6 style="margin-bottom:20px"><a href="<?php the_permalink() ?>">Read more...</h6>
       </div>
       </div>
    
       <?php endwhile; ?>
    
       <?php else : ?>
    
       <h2>Not Found</h2>
    
       <?php endif;
       wp_reset_query();
       ?>
       </div>
       </div>
    <div id="left2">
       <!-- RECENT POSTS QUERY -->
       <?php $pc = new WP_Query('posts_per_page=3&offset=1&orderby=date&order=dec'); ?>
       <?php while (have_posts()) : the_post(); ?>
       <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?> &rsaquo;</a></h2>
       <h6><?php include (TEMPLATEPATH . '/inc/meta.php' ); ?></h6>
       <div class="entry">
       <?php the_excerpt(); ?>
       <h6 style="margin-bottom:20px"><a href="<?php the_permalink() ?>">Read more...</h6>
       </div>
      <?php endwhile;?>
    </div>
  2. Robert Dall
    Member
    Posted 2 years ago #

    Hi

    I would look at Codex for multi-loops

    I would move away from using Query Post and WP_Query instead.

    http://codex.wordpress.org/Class_Reference/WP_Query

    In terms of outdated code… I have been taught that <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?> although it is still correct. using <?php get_template_part('inc/meta'); ?> is the more current way of doing things.

    Hope I have helped… 

  3. Robert Dall
    Member
    Posted 2 years ago #

    Hi Again… 

    I though I would share some code I used on a recent site that had a second query that did reset properly. I hope this code helps you.

    <?php
    		// The Query
    		$front_news = new WP_Query(
    				array(
    					// 'category' => 'name=hot-news',
    					   'posts_per_page' => 4,
    					  ));
    		// The Loop
    		while ( $front_news->have_posts() ) : $front_news->the_post(); ?>
    
    			<li>
    				<h3><a title="News about <?php the_title(); ?>" href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h3>
    				<?php the_excerpt(); ?>
    			</li>
    
    		<?php  // Reset Post Data
    			endwhile; wp_reset_postdata(); ?>
  4. phendyr
    Member
    Posted 2 years ago #

    Thank you for the feedback, I'll give this new code a shot and report back... very much appreciated.

  5. phendyr
    Member
    Posted 2 years ago #

    RDall thank you, your code worked perfectly, I also incorporated your feedback on the call for the template meta.

    Thanks again.

  6. Robert Dall
    Member
    Posted 2 years ago #

    Your welcome… 

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.