简体   繁体   English

显示当前帖子类别Wordpress

[英]Display current posts category Wordpress

On my front-page.php I have a custom loop displaying all posts. 在我的front-page.php上,我有一个显示所有帖子的自定义循环。 I loop through them and display the featured image and title. 我遍历它们并显示特色图像和标题。 I also want to add the category that the post is in but seem to be hitting a brick wall! 我还想添加帖子所在的类别,但似乎正在碰壁砖! It's probably an easy fix but any help would be appreciated. 这可能是一个简单的解决方案,但任何帮助将不胜感激。

Here's my code. 这是我的代码。

    <? $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(); ?>

Thanks in advance :) 提前致谢 :)

Here is the example: 这是一个例子:

<? $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(); ?>

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

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