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

Natasha McDiarmid
11,400 PointsPHP WordPress Function
I'm trying to build a function for a WordPress loop widget. I don't know a ton of PHP – only WordPress hooks.
This is what I have, and it's working, however I don't know if this is following best practices...
I made the loop a function because it will be used throughout the site in multiple areas.
It reads very messy to me.
function widget_feat_opportunity(){
echo '<div class="small-8 medium-8 large-8 columns right">';
echo '<div class="widget-title">' . "Featured Opportunity" . '</div>';
$feat_opportunities = new WP_Query( 'posts_per_page=1&cat=6' );
while ( $feat_opportunities->have_posts() ) : $feat_opportunities->the_post();
echo '<div class="row">' . '<div class="small-4 medium-4 large-4 columns">';
the_post_thumbnail('feat-opportunities');
echo '</div>' . '<div class="small-8 medium-8 large-8 columns">';
the_content();
echo ' </div></div>';
endwhile;
// Jump Link
echo '<p class="jump-link">' . '<a href="" target="_blank">';
echo 'View All Opportunities ≫';
echo '</a></p></div>';
}
Any feedback would be appreciated! Also, this editor isn't allowing me to properly indent or add breaks?...
4 Answers

Jeff Busch
19,287 PointsOops, didn't instantiate a new WP_Query object. Here:
<?php
$feat_opportunities = new WP_Query( 'posts_per_page=1&cat=6' );
while( $feat_opportunities->have_posts()) {
$feat_opportunities->the_post();
?>

Jeff Busch
19,287 PointsHi Natashia,
I don't know anything about Wordpress. I am not a fan of the endwhile method, I prefer curly braces. Also, I try not to echo out static html, more processing time. I'm assuming the "Featured Opportunity" in the first div is just text output to the browser. Another thing, in your third to last echo statement, for example, you could have written:
<?php
echo '<p class="jump-link"><a href="" target="_blank">';
?>
So, if I wrote the function it would probably look like this:
function widget_feat_opportunity() {
?>
<div class="small-8 medium-8 large-8 columns right">
<div class="widget-title">Featured Opportunity</div>
<?php
while( $feat_opportunities->have_posts()) {
$feat_opportunities->the_post();
?>
<div class="row"><div class="small-4 medium-4 large-4 columns">
<?php
the_post_thumbnail('feat-opportunities');
?>
</div><div class="small-8 medium-8 large-8 columns">
<?php
the_content();
?>
</div></div>
<?php
}
?>
<p class="jump-link"><a href="" target="_blank">View All Opportunities ≫</a></p></div>
<?php
}
?>
Jeff
Andrew McCormick
17,730 PointsNatasha McDiarmid , How Jeff framed it above is what we typically do in WP dev work. You'll see a lot of mix php and html with wordpress.
As for the "endwhile", that's the way WP core devs chose to do the loops. almost everyone you'll find uses that structure. There are some discussions out there that endwhile is better than using curly braces when doing a lot of html in the loop. Personally I still prefer the curly braces though and haven't had any trouble.
The next thing you'll run in to is having to use output buffering. I've had several occasions when writing plugins that I needed to make sure all the php processed before it started to try and spit out the html. So you may find yourself needing to use ob_start() when writing a lot of html/php mix in functions, then collecting and returning the contents of the buffering.

Natasha McDiarmid
11,400 PointsThanks for all your help! Echo-ing statements was the only way I knew how to implement HTML into PHP, but I see what you did, that makes sense. However the code above is giving me an error on this line:
while( $feat_opportunities->have_posts()) {

Jeff Busch
19,287 PointsWhat is the error?

Natasha McDiarmid
11,400 PointsThe error is: "Call to a member function have_posts() on a non-object"
Aurelian Spodarec
7,369 PointsAurelian Spodarec
7,369 Pointshi,unfortunately i don't know the answer but this will speed up others. I don't know whats this ;p PHP with mixed HTML? i guess so but try to but it how it was in your code because it will be hard for one that will help you ,
try to watching the tip for asking questions on right or post he that on codepen.io if that would help too. I hope this helps you and others in time .