I think (at least part of) the problem is that you’ve got a nested loop where you don’t really need one.
The output of get_terms()
is an array of objects. So $categories
should be an array of objects.
You loop through this array of objects here:
foreach ( $categories as $category ) {
$id = $category->term_id;
$img = wp_get_attachment_image( $imgs[$id], 'thumbnail' );
}
The problem is that you close that loop, and then loop through $categories
a second time:
for($i=0; $i<count($categories); $i++)
{
echo('<h2>'.$categories[$i]->name.'</h2>');
echo($img);
echo('<p>'.$categories[$i]->description.'</p>');
}
The problem there, though, is that $id
and $img
will retain their value based on the last object in $categories
.
I would recommend getting rid of the second loop, and doing everything in your original foreach
loop:
foreach ( $categories as $category ) {
// Set ID and IMG
$id = $category->term_id;
$img = wp_get_attachment_image( $imgs[$id], 'thumbnail' );
// Output markup
echo('<h2>'.$category->name.'</h2>');
echo($img);
echo('<p>'.$category->description.'</p>');
}
That should fix the issues with out-of-sync looping, though I’m not entirely certain about $img
, since I’m not familiar with the Plugin you’re using and don’t know how it saves its option to the database.