I have an array with three objects:
Ob1
Ob2
Ob3
I tried the following:
$args = array('child_of' => 184);
$categories = get_the_category($post->ID, $args);
$i = 0;
$len = count($categories);
foreach($categories as $cat) {
if ($i == 0) {
echo '<li><h2><a href="'.get_category_link($cat->cat_ID).'">'.$cat->name.', </a></h2></li>';
} else if ($i == $len - 2) {
echo '<li><h2><a href="'.get_category_link($cat->cat_ID).'">'.$cat->name.'</a></h2></li>';
}
$i++;
}
But I get
Ob1, Ob2
Basically if it is the last item I don't want the comma but I am not sure what is wrong with that code and why it is showing only two values.
If I do:
var_dump($len);
It gives me int(3)