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

PHP

Rhoda Toynbee
Rhoda Toynbee
18,938 Points

PHP Error on single-work.php page

I am working through the Wordpress Theme development. I have my site migrated over to my live site and this is the error I am getting on my single work page:

Warning: Invalid argument supplied for foreach() in /home/rhodades/public_html/wp-content/themes/rhodadesignstudio/single-work.php on line 31

I have tried a few things, and can't figure out what the error is :(

        <?php 

        $testimonials = get_field( 'related_testimonials' ); 

        foreach ($testimonials as $testimonial);
            $name = get_field( 'name', $testimonial->ID );      
            $testimonial = get_field( 'testimonial', $testimonial->ID ); ?>

        <div class="testimonial push_2 grid_10 clearfix">
            <blockquote>&ldquo; <?php echo $testimonial; ?> &rdquo;</blockquote>
            <cite>&mdash; <?php echo $name; ?></cite>
        </div>

        <?php endforeach; ?>

I noticed that if I put in a testimonial then the error goes away...

Also, my project images aren't showing up on my "view work" page when I click on an individual project. Ideas?

Thanks in advance :)

Rhoda

3 Answers

I believe the error stems from having a semi-colon at the end of your foreach constructor. Instead, you should construct it with curly braces (since you're executing more than statement within the foreach constructor). So, I changed the php to end at the very end of the foreach constructor, and so so as you loop through $testimonials, you'll echo out the HTML to the page with the variables right inside the echo statement.

<?php 

        $testimonials = get_field( 'related_testimonials' ); 

        foreach ($testimonials as $testimonial) {
            $name = get_field( 'name', $testimonial->ID );      
            $testimonial = get_field( 'testimonial', $testimonial->ID );

            echo '<div class="testimonial push_2 grid_10 clearfix">
                  <blockquote>&ldquo;$testimonial&rdquo;</blockquote>
                  <cite>&mdash;$name</cite>
                  </div>';
        }
?>

I hope that helps!

Rhoda Toynbee
Rhoda Toynbee
18,938 Points

I tried that on the page and it broke it as well :/

I'm not sure what the problem is. I deleted the <?php endforeach; ?> and entered a quick testimonial in the custom page of my admin panel.

The error is gone, but the image of the project still doesn't show up.

Try to grab what I posted again, because I might have been making some edits while you were copying. I realized that I had made an error on where I put the ending curly brace for the foreach constructor and I had edited the echo line.

Rhoda Toynbee
Rhoda Toynbee
18,938 Points

I tried it again. Same problem. It just kills the page. I'm thinking it's somewhere else in my code :(

I've made it work with this:

<?php 
        $testimonials = get_field( 'related_testimonials' ); 

        foreach ($testimonials as $testimonial);
            $name = get_field( 'name', $testimonial->ID );      
            $testimonial = get_field( 'testimonial', $testimonial->ID ); ?>

        <div class="testimonial push_2 grid_10 clearfix">
            <blockquote>&ldquo; <?php echo $testimonial; ?> &rdquo;</blockquote>
            <cite>&mdash; <?php echo $name; ?></cite>
        </div>

Everything is good as long as I have a testimonial attached to it. One project does. One doesn't, so there is an error line on that page.

http://rhodadesignstudio.com/work/moments-sara-jean/

Perhaps I am totally wrong. My PHP skillz aren't the greatest; I am just aware of the syntax. I hope someone can help you out here better than I can. Good luck, Rhoda! Your site looks very nifty by the way. :)

Rhoda Toynbee
Rhoda Toynbee
18,938 Points

Thanks! and thank you for your help... PHP is my Achilles heel as well :) I understand what it's doing, but can't seem to tinker with it the way I can other code. Someday...

Always glad to help! :)