Wordpress - 回声类别 - 循环中的 Slugs

Wordpress - Echo Category-Slugs in Loop

我试图在 archive.php

的 Wordpress-Loop 中回显多个 catgory-slugs 而不是类别名称

我的代码看起来像这样,到目前为止我尝试了很多,但似乎没有任何效果。我所取得的成就是获得了一个类别的第一个 slug,但不是所有的:

    <li class="<?php the_category( ' ' ); ?> ">
        <div class="content">
            <h4><?php the_title(); ?></h4>
        </div>
    </li>

下面的代码应该可以工作

<?php
$categories = get_the_category();
$cls = '';

if ( ! empty( $categories ) ) {
  foreach ( $categories as $cat ) {
    $cls .= $cat->slug . ' ';
  }
}
?>
<li class="<?php echo $cls; ?> ">
    <div class="content">
        <h4><?php the_title(); ?></h4>
    </div>
</li>