[Support request] Insert PHP to Hook

Home Forums Support [Support request] Insert PHP to Hook

Home Forums Support Insert PHP to Hook

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1644481
    Felix

    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

    #1644879
    Elvin
    Staff
    Customer Support

    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

    A wise man once said:
    "Have you cleared your cache?"

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.