Passing variables into a foreach loop in a template

Controller converts the cameCase of your method names into snake_case when it turns them into variables that are made available to your blades. You’d have to ask @withjacoby why exactly it does that.

As for your htmlspecialchars error: get_term_by returns an object, which you assigned to name. When you try and access it with {{ $child->name }} you’re attempting to treat an object as a string, and htmlspecialchars() chokes on that.

You can probably solve that issue by making the following change:

foreach ( $term_children as $child ) {
    $subcategory = (object) array(
        // get the name from what `get_term_by()` returns:
        'name' => get_term_by( 'id', $child, $taxonomy_name )->name,
        'link' => get_term_link( $child, $taxonomy_name ),
    );

    array_push($info, $subcategory);
}
2 Likes