简体   繁体   English

将自定义分类术语添加为类

[英]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.我有一个自定义分类“thema”,现在我想将相应的主题术语作为类添加到存档页面上的每个帖子中。

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.我可以列出帖子的所有术语,但是当我想将 output 作为类时,页面会从帖子循环开始的位置停止加载。

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.希望能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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