admin管理员组文章数量:1429798
I'm trying to display category name inside a foreach post ..
<?php
$recent_posts = wp_get_recent_posts(array(
'post_status' => 'publish',
'cat' => '',
));
foreach($recent_posts as $post) : ?>
<div class="card">
<div class="card-bg">
<div class="card-cat">
<?php foreach((get_the_category()) as $category) {
echo $category->name.'';
} ?>
</div>
<img class="card-img" src="<?php echo get_the_post_thumbnail_url( $post['ID'] ); ?>">
</div>
<div class="card-body">
<div class="card-title">
<?php echo $post['post_title'] ?>
</div>
</div>
</div>
<?php endforeach;
wp_reset_query(); ?>
Does not work !
I'm trying to display category name inside a foreach post ..
<?php
$recent_posts = wp_get_recent_posts(array(
'post_status' => 'publish',
'cat' => '',
));
foreach($recent_posts as $post) : ?>
<div class="card">
<div class="card-bg">
<div class="card-cat">
<?php foreach((get_the_category()) as $category) {
echo $category->name.'';
} ?>
</div>
<img class="card-img" src="<?php echo get_the_post_thumbnail_url( $post['ID'] ); ?>">
</div>
<div class="card-body">
<div class="card-title">
<?php echo $post['post_title'] ?>
</div>
</div>
</div>
<?php endforeach;
wp_reset_query(); ?>
Does not work !
Share Improve this question asked May 4, 2019 at 11:55 warzonemasterwarzonemaster 51 silver badge3 bronze badges1 Answer
Reset to default 1Please try below updated code
<?php
$recent_posts = wp_get_recent_posts(array(
'post_status' => 'publish',
'cat' => '',
));
foreach($recent_posts as $post) : ?>
<div class="card">
<div class="card-bg">
<div class="card-cat">
<?php
$category_detail=get_the_category($post['ID']);//Pass POST ID
foreach($category_detail as $cd){
echo $cd->cat_name.'';
}
?>
</div>
<img class="card-img" src="<?php echo get_the_post_thumbnail_url( $post['ID'] ); ?>">
</div>
<div class="card-body">
<div class="card-title">
<?php echo $post['post_title'] ?>
</div>
</div>
</div>
<?php endforeach;
wp_reset_query(); ?>
Try and let me know if any query.
Hope it will help!
本文标签: phpHow to get category name or ID by post
版权声明:本文标题:php - How to get category name or ID by post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745524069a2661754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论