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'm working on a project where I need display 2 post from one category and also I need to put another loop between this post post to display random list of post from other category. Easiest way to do so is to create 3 queries, but when I created 2 queries for one category, but here I got problem one loop breaks and display both posts, second works fine. I used offset parameter for second query but it doesn't work.

<?php

        $first_query_args = array(
          'category_name' => 'first-category',
          'tag' => 'special-tag',
          'post_per_page' => 1,
        );

        $first_query = new WP_Query( $first_query_args );

        if ( $first_query->have_posts() ) : while ( $first_query->have_posts() ) : $first_query->the_post();

        // First query stuff goes here

        endwhile; endif;

        wp_reset_query(); 

        // Query for Random posts
        $query_for_random_args = array(
            'category_name' => 'cat-for-rand',
            'post_per_page' => 3,
            'orderby' => 'rand',
        );

        $query_for_random = new WP_Query( $query_for_random_args );

        if ( $query_for_random->have_posts() ) :
    ?>

        <ul>

        <?php while ( $query_for_random->have_posts() ) : $query_for_random->the_post(); ?>

            <li>
                <?php if ( has_post_thumbnail() ) { ?>
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'list-image' ); ?></a>
                    <a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a>
            </li>

        <?php } endwhile; ?>

        </ul>

        <?php endif; wp_reset_query(); ?>

    <?php


        $second_query_args = array(
          'category_name' => 'first-category',
          'tag' => 'special-tag',
          'offset' => 1,
          'post_per_page' => 1,
        );

        $second_query = new WP_Query( $second_query_args );

        if ( $second->have_posts() ) : while ( $second_query->have_posts() ) : $second_query->the_post();

        // Second query stuff goes here

        endwhile; endif;
?>

( This code What I use for those loops )

Any ideas how can I solve this problem? or how can I use 2 queries for similar task?

Cheers

George

share|improve this question
    
Show the code you tried = best. –  kaiser May 18 '11 at 16:15
add comment

1 Answer 1

up vote 1 down vote accepted

I would use get_posts() for your two ad hoc Loops. (Actually, you only need one ad hoc Loop, if you're modifying the main Loop so that only 2 posts are retrieved.) e.g.

\\ code modify main Loop to return only 2 posts goes here

$firstpost = true;

if ( have_posts() ) : while ( have_posts() ) : the_post();

    \\ main Loop Post output goes here

    if ( $firstpost ) {

        $randomposts = get_posts( array( 'category' => $categoryid, 'numberposts' = $numberposts, 'orderby' => 'rand' );

        \\ Output ad-hod Loop content, e.g.
        ?>
        <ul>
        <?php
        foreach ( $randomposts as $randompost ) { ?>
            <li><a href="<?php echo get_permalink( $randompost->ID ); ?>"><?php echo $randompost->post_title; ?></a></li>
        <?php } ?>
        </ul>
    <?php }

   $firstpost = false;

// end the main Loop
endwhile; endif;

(Untested example code)

share|improve this answer
    
Hello Chip. Thanks for help. Yes I know that I can modify main loop with query_post. I thins situation I'm more interested if I can inset loop for random post like people use to insert google ads after first post or between posts –  Mamaduka May 18 '11 at 21:07
    
@Mamaduka yes, you can. That's what the $randomposts = get_posts() business is. –  Chip Bennett May 18 '11 at 23:20
    
@Chip Bennett I tested your code example works great, but it outputs random post after each post from main loop. It great if you want place content after each post, in this example I only want to display random post after fist post –  Mamaduka May 19 '11 at 9:12
    
@Mamaduka I may have misread your OP. I assumed there would only be two posts in the main Loop. You could always add a variable, and only output the ad hoc Loop if the variable isn't set. Let me update my answer to give an example. –  Chip Bennett May 19 '11 at 12:39
    
(Answer updated per above comment.) –  Chip Bennett May 19 '11 at 12:44
show 1 more comment

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.