-2

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>";
    }
  • Don’t just say “the page breaks”, that is not a proper problem description. – CBroe Apr 27 '21 at 08:02
  • 1
    I don’t see you even attempting to output the terms as _classes_ of any element there? You are just echo-ing them, outside of the post element HTML …? – CBroe Apr 27 '21 at 08:03
  • Also, using the tag _slugs_ instead of names, would probably be “safer” in terms of the possibility of not creating invalid classes. – CBroe Apr 27 '21 at 08:04
  • That's where I'm stuck. When I try to output the terms as classes on the div with the post-class, the posts aren't loading. – Thessa Verbruggen Apr 27 '21 at 08:04
  • What’s the point of telling us about errors produced by code you have not even actually shown us? Show us what exactly you tried then, and not code that does something else. And what did you do to try and debug this so far? – CBroe Apr 27 '21 at 08:07
  • I found the problem - `echo $t->name, ' ';` replace `,` with `.` – Dmitry Leiko Apr 27 '21 at 08:08
  • I modified the code how I tested it and when it produced an error. – Thessa Verbruggen Apr 27 '21 at 08:13
  • @Dmitry no, that syntax is perfectly valid for `echo`. https://www.php.net/manual/en/function.echo.php: _“Its arguments are a list of expressions following the echo keyword, separated by commas”_ – CBroe Apr 27 '21 at 08:15
  • You can not “concatenate” a `foreach` loop into a text literal. End the echo statement, then do the foreach, then use a second echo for everything that is supposed to come after. – CBroe Apr 27 '21 at 08:16
  • (If you want to keep on echo-ing everything, that is. The syntax explained in https://www.php.net/manual/en/language.basic-syntax.phpmode.php is usually preferable IMHO, if large parts of the output are static, and only certain places need dynamic values inserted.) – CBroe Apr 27 '21 at 08:18
  • @CBroe could you tell me what I need to do? I understand what you're saying but I don't know how to change it. That's why I put the forearch loop out of the text literal, but I don't know how to echo the output inside it. – Thessa Verbruggen Apr 27 '21 at 08:23
  • Did I not tell you already? How hard can it be, to just do `echo 'something'; foreach(…) { echo $termstuff; } echo 'another thing';` …? – CBroe Apr 27 '21 at 08:26

1 Answers1

1

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.

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42