简体   繁体   中英

Add custom taxonomy terms as classes

I'm having trouble displaying the post taxonomy terms as classes. I know I've done it in the past, but I can't seem to find it and don't remember how to do it.

I have a custom taxonomy 'thema', now I want to add the corresponding theme term as classes to every post on the archive page.

I can list all the terms for a post, but when I want to output it as classes, the page stop loading from the point where the post loop starts.

This is what I have so far:

(EDIT: changed some code to show when the error occurs)

while ( $query->have_posts() ) {
        $query->the_post();
        $f = get_fields();

        $link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());

        $terms = wp_get_post_terms( get_the_ID(), 'thema');


        echo "<div class='post ". foreach ($terms as $t) { echo $t->slug, ' '; } ."'>";
            echo "<div class='postWrapper'>";
                echo "<div class='content'>";
                    echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";

                    echo the_excerpt_max_charlength($charlength = 130);
                    echo "<a href='".$link."' title='Ga naar ".get_the_title()."' class='link'>Lees meer</a>";
                echo "</div>";
            echo "</div>";
        echo "</div>";
    }

You can try this way:

$classes = '';
$terms = wp_get_post_terms( get_the_ID(), 'thema');

foreach($terms as $t) {
    $classes .= $t->slug . ' ';
}

echo "<div class='post ". $classes ."'>";

It's should work. Hope help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM