Archived topic
Insert PHP to Hook
1 reply · Started by Felix on February 3, 2021
hey Team!
some time ago i put some php to a hook and it worked pretty well.
<?php $terms = get_the_terms( $post->ID , 'category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
?>
now i have tried it again with the same snip and get this error:
Invalid argument supplied for foreach() in /Local Sites/generate-press-test/app/public/wp-content/plugins/gp-premium/elements/class-hooks.php(196) : eval()'d code on line 13
any suggestions? maybe there is a better way to archive my plan. :)
Thanks
Hi there,
As the error mentioned, there's a potential issue with your $terms.
get_the_terms() doesn't always return an array. If in case it did actually not return an array, foreach would show this error.
See this: https://developer.wordpress.org/reference/functions/get_the_terms/#return
That said, consider trying this solution. https://wordpress.stackexchange.com/a/209975