Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Mahesh Sangam
Mahesh Sangam
71 Points

How can i use foreach loop in wordpress to make my postings one float left and other one float right, please kindly help

I have worked to make my post to display on my page with foreach loop so i can place my posts one in float left and one float right, but the problem is both are floating right!! and my ++ is not working i think please kindly some one help me... it's my runing project and i need to finish with this post....

<?php   $args = array(
'posts_per_page'=>6,
'orderby' => 'rand',
//'category_name' => 'ourhistory'
);                      

$myposts = get_posts( $args );

echo '<ul>';
echo '<li class="history-year wow fadeIn"><span>'. date('Y').'</span></li>';
foreach ( $myposts as $post ) : setup_postdata( $post == 2 );
if($myposts % 2 == 0){
        echo '<li class="history-block-left history-block-1 wow fadeInLeft">'. the_post_thumbnail('ourhistory-mug') . ' <h3>' . get_the_title() . '</h3> <p>' . the_content('read more...') . 
'</p> </li>';
        } else {
            echo '<li class="history-block-right history-block-2 wow fadeInRight">' . the_post_thumbnail('ourhistory-mug') . ' <h3>' . get_the_title() . '</h3> <p>' . the_content('read 
more...') . '</p> </li>';
            }
            $myposts++;

endforeach;
echo '</ul>';
/*Restore original post data*/
wp_reset_postdata();
?>

8 Answers

Zack Klinger
Zack Klinger
17,622 Points

I'm just trying to help here, but the value of $myposts doesn't change, but $post does. So $mypost % 2 will always give you the same value. $post % 2 is the value that would be changing, correct? You also check $post == 2, did you mean to evaluate it or set it? ($post = 2)

Zack Klinger
Zack Klinger
17,622 Points

I see a little more clearly, you are incrementing $mypost within the foreach loop, but $mypost is part of the conditional statement. It seems like the logical thing to do is check a separate counter variable, like set int count = 0; before the loop, and then ++count within the foreach to increment it. Then, count % 2 would give an alternating True/False value.

Zack Klinger
Zack Klinger
17,622 Points

I have the answer to the ++ question...

Putting the '++' after the variable is called a postfix increment. It adds the +1 after it evaluates the variable. So in essence, you are displaying the current value, and then adding +1 to it for the next instance of that variable.

You can instead prefix the '++' (which is also faster in memory terms) by writing ++var, instead of var++. This will do what I think you really want, which is add +1 to the variable, and then use the new value.

Hope that helps!

Mahesh Sangam
Mahesh Sangam
71 Points

Zack Klinger Sir, I added +$myposts but it's not working fine in my wordpress...

Fatal error: Unsupported operand types in C:\xampp\htdocs\wordpresssites\testing\wp-content\themes\layout-3-wp-theme\front-page.php on line 188

PLease kindly will you write full script again on this chat please.... i need this urgently please please...

Mahesh Sangam
Mahesh Sangam
71 Points

Now i used ++$myposts No error but the problem is everything floating right.... i need one float left and other one float right...

I think My if($myposts % 2 == 0){ echo '<div style="float:left">.........</div>'; } else{ echo ''<div style="float:right">.........</div>'; } IN this code it;s working by Float right only i think it;s not working by echo if % by 2 == 0....

Need to check full code and please re-write full perfect code using my code.... Please Please

Mahesh Sangam
Mahesh Sangam
71 Points

let me try thanks a lot!

Mahesh Sangam
Mahesh Sangam
71 Points

no sir it;s not working now all are right side i need alernative one at left and one at right

Zack Klinger
Zack Klinger
17,622 Points

Did you see my comment above about using a separate variable like $count instead of incrementing $myposts?

Sorry I can't be of more help...

Mahesh Sangam
Mahesh Sangam
71 Points

ZAck Klinger It's DONE!!! WOW THIS IS Working Great JOB!!!

Zack Klinger
Zack Klinger
17,622 Points

I'm used to C++ style of for and while loops...

for (int i = 0; i < count; ++i) { //stuff }

or...

int count = 0; while (count < 10) { //stuff ++count; }

Oh boy, that was really a shot in the dark on my part. Glad it helped!

Mahesh Sangam
Mahesh Sangam
71 Points

You really helped me on perfectly Thanks a lot Sir.... Im Very Much Glad that you really quick on your answers!!...