显示当前帖子类别 Wordpress

Display current posts category Wordpress

在我的 front-page.php 上,我有一个显示所有 post 的自定义循环。我循环浏览它们并显示特色图片和标题。我还想添加 post 所在的类别,但似乎碰壁了! 这可能是一个简单的修复,但我们将不胜感激。

这是我的代码。

    <? $posts = get_posts(); 
    foreach($posts as $post) : setup_postdata($post); ?>

        <? global $post; $categories = get_the_category($post->ID);?>

        <div class="col-sm-6 col-md-4">
            <div <?php post_class( 'category-wrapper' ); ?>>
                <div class="post-image">
                    <? if (has_post_thumbnail()) {
                        the_post_thumbnail();
                    } ?>
                </div>
                <div class="post-content">
                    <p class="title"><? the_title(); ?></p>
                    <p class="cat-title"><?= $categories['cat_name']; ?></p>
                </div>
            </div>
        </div>

    <? endforeach; wp_reset_postdata(); ?>

提前致谢:)

示例如下:

<? $posts = get_posts(); 
foreach($posts as $post) : setup_postdata($post); ?>

    <? global $post; $categories = get_the_category($post->ID);?>

    <div class="col-sm-6 col-md-4">
        <div <?php post_class( 'category-wrapper' ); ?>>
            <div class="post-image">
                <? if (has_post_thumbnail()) {
                    the_post_thumbnail();
                } ?>
            </div>
            <div class="post-content">
                <p class="title"><? the_title(); ?></p>
                <? foreach( $categories as $category ): ?>
                    <p class="cat-title"><?= $category->cat_name; ?></p>
                <? endforeach; ?>
            </div>
        </div>
    </div>

<? endforeach; wp_reset_postdata(); ?>