get_the_terms() returning wrong results inside of loop

When I use get_the_terms() inside a loop, the whole thing behaves very strangely. Sometimes the terms are included twice or three times and sometimes a post inside of the loop returns only false.

I have used get_the_terms() over and over for the last few years without any problems. So i'm quite confused right now.

It seems like wp_get_post_terms() and wp_get_post_categories() are getting out the correct data but I would like to know why get_the_terms() is behaving this way inside of a loop.

Can someone explain that to me?

Edit: Here is my code for the category badges. Btw: I'm using sage from roots.io so i'm working with blade templates. It happens on custom queries and default main queries (like: index.php, archive.php etc.)

index.blade.php

@if (have_posts())

    @while(have_posts()) @php(the_post())

        @includeFirst(['partials.content-' . get_post_type(), 'partials.content'])

    @endwhile

@endif

partials/content.blade.php

@php

    $post_id = get_the_ID();
    $terms = get_the_terms( $post_id, 'category' );

@endphp

@if($terms  ! is_wp_error( $terms ))
    div class=badges
        @foreach($terms as $term)
            span class=badge{!! $term-name !!}/span
        @endforeach
    /div
@endif

Topic terms loop theme-development Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.