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 trialPierre Smith
11,842 PointsPHP For loops in wordpress
I'm attempting to build some tabs within my posts page for each separate category but I keep getting these errors.
Use of undefined constant i - assumed 'i' in /Users/kipp0/Sites/wordpress/pssp.on.ca/wp-content/themes/pssp/home.php on line 40
Notice: Undefined index: i in /Users/kipp0/Sites/wordpress/pssp.on.ca/wp-content/themes/pssp/home.php on line 40
Notice: Trying to get property of non-object in /Users/kipp0/Sites/wordpress/pssp.on.ca/wp-content/themes/pssp/home.php on line 40
here's my code:
<?php
echo '<ul class="nav nav-tabs" role="tablist">';
args = array(
'hide_empty'=> 1,
'orderby' => 'slug',
'order' => 'ASC'
);//this is good.
$categories = get_categories($args);
$categories_length = count($categories);
for ( $i = 0; $i < $categories_length; $i++ ) {
$cat_name = $categories[i]->name;
if( $i == 0 ) {
echo '<li role="presentation" class="active"><a href="#' .$cat_name.'" aria-controls="'.$cat_name.'" role="tab" data-toggle="tab">' .
$cat_name.'</a></li>';
} else {
echo '<li role="presentation"><a href="#' .$cat_name.'" aria-controls="'.$cat_name.'" role="tab" data-toggle="tab">' .
$cat_name.'</a></li>';
}
}
echo '</ul>';
echo '<div class="tab-content">';
for ($i = 0; $i < count($categories); $i++) {
$cat_name = $categories[i]->name;
$cat_slug = $categories[i]->slug;
if($i == 0) {
echo '<div class="tab-pane active" id="' . $cat_name.'">';
} else {
echo '<div class="tab-pane" id="' . $cat_name.'">';
}
$the_query = new WP_Query(array(
'post_type' => 'news',
'posts_per_page' => 5,
'category_name' => $cat_slug
));
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<h3><a href="'.the_permalink().'">'.the_title().'</a></h3>
<p><em>'. the_time('l, F, jS, Y').' by '. the_author() .'</em></p>';
the_excerpt();
echo '<hr/>';
endwhile;else:
echo 'There are no posts.';
endif;
echo '</div>';
}
echo '</div>';
?>
2 Answers
Zac Gordon
Treehouse Guest TeacherIt looks like the for loop may not be being closed here properly before the Loop starts.
Zac Gordon
Treehouse Guest TeacherBesides changing the $i variable did you change some of the structure and logic of your code?
Pierre Smith
11,842 PointsI don't think so. I think it has something to do with accessing the categories through w/ a index.
On a second note: do you mind explaining how to add a previous post link to this that loads the next 5 posts within it's category. please. ---- I figured it out.
Pierre Smith
11,842 PointsPierre Smith
11,842 PointsThank you for your reply!
I'm closing the for loop after the if while loop because I'm looping through each category as well as all my posts. I have a close for the first for loop before the "echo '</ul>';" and one before the second last "echo '</div>';"
is it possible i'm grabbing the index for categories wrong?
I got it to work this way. Is it possible you could explain why? sorry If this may seem a bit much.