Mambley
Mambley

Reputation: 15

Wordpress Multiple Loop

Which is the best solution when we have multiple loops in the same page? I am using for the main loop this:

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

<?php endwhile; endif; ?>

Now i am trying to add a new loop in the same page (in a different location) for featured posts in a specific category name, which is for you the best choice: ("content" is just for example)

1- Use get_posts();

<?php global $post;
$args = array( 'category_name' => 'destaques' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>

   "content"

<?php endforeach; ?>

2- Use WP_Query();

<php $my_query = new WP_Query("category_name=destaques");
while ($my_query->have_posts()) : $my_query->the_post(); ?>

   "content"

<?php endwhile; ?>

3: Use query_posts();

<?php query_posts( 'category_name=destaques' );
if (have_posts()) : while (have_posts()) : the_post(); ?> 

   "content"

<?php endwhile; endif; ?>

Which you choose and why?

Thanks.

Upvotes: 0

Views: 845

Answers (3)

jocap
jocap

Reputation: 476

I'd use number 3. It's easier to read and understand.

Upvotes: 0

Kory
Kory

Reputation: 398

The optimal choice is get_posts();

Here is the reasoning from the Wordpress Function Reference for query posts:

The query_posts() function is intended to be used to modify the main page loop only. It is not intended as a means to create secondary loops on the page. If you want to create separate Loops outside of the main one, you should use get_posts() instead. Use of query_posts() on loops other than the main one can result in your main loop becoming incorrect and possibly displaying things that you were not expecting.

The query_posts() function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

The query_posts() function creates a new WP_Query object and assign it to global wp_query variable. The get_posts() function creates a new WP_Query object without overriding anything in the global area.

Upvotes: 3

Marty
Marty

Reputation: 4657

Im not sure about others but my choice is most often or not

<?php 
query_posts('showposts=1&cat=-48'); // our custom query
if ( have_posts() ) : while ( have_posts() ) : the_post();  // Start the loop
    $img = get_post_meta($post->ID, "postimage", $single = true);//any custom fields?
?>
    <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>">
      <img src="<?php echo $img; ?>" alt="<?php the_title(); ?>" />
    </a>
<?php
endwhile; endif;// End the Loop and Check for Posts
wp_reset_query(); // Reset the loop
?>
<div>stuff</div>
<?php 
query_posts('showposts=5&cat=5'); // our custom query
if ( have_posts() ) : while ( have_posts() ) : the_post();  // Start the loop
    $img = get_post_meta($post->ID, "postimage", $single = true);//any custom fields?
?>
    <h2>Title</h2>
    etc..etc..
<?php
endwhile; endif;// End the Loop and Check for Posts
wp_reset_query(); // Reset the loop
?>

this proves to work fine for my needs...

Upvotes: 0

Related Questions