0

Specifically, I am bringing back the 7 most recent posts in Category A, but then I want to put the first posts thumbnail in <div id=A> and the second post's thumbnail in <div id=B> and so on for the seven posts.

If this was an array, I would simply use the array position inside of the <div>, but I'm not sure how to access the individual posts once I have brought them back with query_posts.

Any ideas?

0

Why not just use a counter and then conditional logic?

$i = 0;
while($loop->have_posts()) : $loop->the_post();
    if($i === 0){
        echo '<div class="a">'.other_stuff().'</div>';
    } elseif($i === 1){
        echo '<div class="b">'.other_stuff().'</div>';
    } else {
        echo '<div class="default">'.other_stuff().'</div>';
    }
    $i++;
endwhile;
  • $loop->current_post prevent the use of $i and a switch statement normally is faster than seven if / elseif – gmazzap Aug 20 '13 at 19:58
  • Thanks, G.M. That might work as I could also inject other HTML around each of the if/then or switchs. I will try it and get back. – ericgr Aug 20 '13 at 20:15
  • seven? i was told 2! and i've never had an issue using $i – GhostToast Aug 20 '13 at 20:20
  • I actually meant, thanks to GhostToast and G.M. GT, "7" is in the first sentence. I'm actually working on combining both of ideas. – ericgr Aug 20 '13 at 20:25
  • @GhostToast $i is so innocent... I don't think it can ever bring an issue, the mine was just a tip, because current_post is so comfortable – gmazzap Aug 20 '13 at 21:47
2

First, in general it's better to use WP_Query vs query_posts as query_posts modifies the main loop while WP_Query generates a secondary loop and causes less messiness later on.

PHP will also advance alpha variables for you with a simple ++. For example:

$div_id = 'a';
$div_id++; // $div_id now equals 'b'

For your example:

$args = array("your args here");
$my_query = new WP_Query($args);
$div_id = 'a';
if($my_query->have_posts()) {
    while($my_query->have_posts()) {
        $my_query->the_post();
        echo '<div id="'.$div_id.'">'.the_content().'</div>';
        $div_id++;
    }
}
  • Thanks, Andrew. I know that I can loop through with the 'while' and output seven identical html code blocks. However, div A is somewhere on the page and div B is somewhere else on the page surrounded by a different html block and div C is surrounded by yet a different html block. So I thought it would be easiest to access it like an array DivA=arr[1].the_title, (that is shorthand html!). Also thanks for the heads up on WP_Query. – ericgr Aug 20 '13 at 20:14
0

just to add to the answers already here, there's no reason why you have to output results within a control structure. the_post() is what advances the internal current_post counter and sets up the global $post variable with the current post's data. with this in mind, you can access any post within a query's results in any order-

$query = new WP_Query( $args );

$query->the_post();

the_content(); // outputs content from first post

$query->the_post();

the_content(); // outputs content from second post

$query->current_post = -1;

$query->the_post();

the_content(); // outputs content from first post again

$query->current_post = 8;

$query->the_post();

the_content(); // outputs content from 10th post
  • Thank you, Milo. I will have to take a look at this and reformat my code as needed. It seems like what I needed (but was already well underway with the other solution) – ericgr Aug 21 '13 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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