在我的front-page.php上,我有一个显示所有帖子的自定义循环。我遍历它们并显示特色图像和标题。我还想添加帖子所在的类别,但似乎正在碰壁砖! 这可能很容易解决,但任何帮助都会受到赞赏。
这是我的代码。
<? $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(); ?>
提前致谢:)
答案 0 :(得分:0)
以下是示例:
<? $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(); ?>