0

This question already has an answer here:

php rookie here. as grids are popular right now, I wonder if this is even possible. I have three posts and have no idea how to get this working; this is my basic loop

                    // WP_Query arguments
                    $args = array ( 'posts_per_page' => 3, 'category__in' => array(4,5)
                    );

                    // The Query
                    $querytest = new WP_Query( $args );

                    // The Loop
                    if ( $querytest->have_posts() ) {
                        while ( $querytest->have_posts() ) {
                        $querytest->the_post();

                        // do something
                        get_template_part( 'content', 'front', get_post_format());

                        }

                    } else {
                    // no posts found
                    }

html output should be something like this:

            <div class="width-1-2"><!-- post 1 goes here --></div>
            <div class="width-1-2">
                <div class="row">
                    <div class="width-1-2"><!-- post 2 goes here --></div>
                    <div class="width-1-2"><!-- post 3 goes here --></div>
                </div>
            </div>

is there any chance this might work somehow?? I tried counting posts, but all I get was errors or totally wrong html output.

thank you, A.

marked as duplicate by kaiser Feb 4 '14 at 0:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Just for better understanding: Is this fixed on three posts and the above layout? Or should this be a generic/dynamic solution? I don't see a real problem here... – tfrommen Feb 3 '14 at 23:45
  • should be generic/dynamic solution. – alexx_slo Feb 3 '14 at 23:57
0

You don't have to run through The Loop in one go.

$args = array(
    'posts_per_page' => 3,
    'category__in' => array(
        4,
        5,
    ),
);

$querytest = new WP_Query($args);

if ($querytest->have_posts()) {
    // this is the first post, with its markup
    $querytest->the_post();
    ?>
    <div class="width-1-2">
        <?php get_template_part('content', 'front', get_post_format());
    </div>
    <div class="width-1-2">
        <div class="row">
            <?php
            // here com the second and third post, wrapped in their additional markup
            for ($i = 0; $i < 2; ++$i)
                if ($querytest->have_posts()) {
                    $querytest->the_post();
                    ?>
                    <div class="width-1-2">
                        <?php get_template_part('content', 'front', get_post_format());
                    </div>
                    <?php
                }
            ?>
        </div>
    </div>  
    <?php
} else {
    // no posts found
}

I hope this gives you an idea and gets you started customizing this to suit your needs.

  • thank you, I am going to try it out in the morning. Guess I don't fully understand, how loop works. – alexx_slo Feb 4 '14 at 0:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.