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

Pierre Smith
Pierre Smith
11,842 Points

PHP 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
STAFF
Zac Gordon
Treehouse Guest Teacher

It looks like the for loop may not be being closed here properly before the Loop starts.

Pierre Smith
Pierre Smith
11,842 Points

Thank 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.

<ul class="nav nav-tabs" role="tablist">
                                <?php 
                                    $cat_args =  array(

                                        'hide_empty'=> 1,
                                        'orderby' => 'slug',
                                        'order' => 'ASC'

                                    );//this is good.
                                    $categories = get_categories($cat_args);
                                    $count = 0;
                                 ?>

                                <?php foreach($categories as $category) : ?>
                                    <?php 
                                        $cat_name = $category->name; 
                                        $cat_slug = $category->slug; 
                                    ?>

                                    <?php if($count == 0 ) : ?>

                                        <li role="presentation" class="active"><a href="#<?= $cat_slug; ?>" aria-controls="<?= $cat_slug; ?>" role="tab" data-toggle="tab"><?= $cat_name; ?></a></li>


                                    <?php else: ?>

                                        <li role="presentation"><a href="#<?= $cat_slug; ?>" aria-controls="<?= $cat_slug; ?>" role="tab" data-toggle="tab"><?= $cat_name; ?></a></li>

                                    <?php endif; ?>

                                    <?php $count += 1; ?>

                                <?php endforeach; ?>

                            </ul>

                            <?php $count = 0; ?>

                            <div class="tab-content" style="padding-top:20px;">
                                <?php foreach ($categories as $category): ?>
                                <?php 

                                    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                                    $cat_slug = $category->slug;
                                    $the_query = new WP_Query(array(

                                      'post_type' => 'post',
                                      'posts_per_page' => 5,
                                      'offset'=> 1,
                                      'category_name' => $cat_slug
                                 ));

                                ?>

                                    <?php if($count == 0 ) : ?>
                                        <div class="tab-pane active" id="<?= $cat_slug; ?>">
                                    <?php else: ?>
                                        <div class="tab-pane" id="<?= $cat_slug; ?>">
                                    <?php endif; ?>


                                    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                                        <h3><a href="<?php the_permalink(); ?>"><?= the_title(); ?></a></h3>
                                        <p><em><?= the_time('l, F, jS, Y') ?> by <?php the_author() ?></em></p>
                                        <?= the_excerpt(); ?>

                                        <hr/>

                                    <?php endwhile; ?>
                                    <?php $count += 1; ?>
                                </div>
                                <?php endforeach; ?>
                                </div>

                            <?php wp_reset_postdata(); ?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Besides changing the $i variable did you change some of the structure and logic of your code?

Pierre Smith
Pierre Smith
11,842 Points

I 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.