gpt4 book ai didi

php - 在 while 循环中获取当前帖子类别名称

转载 作者:行者123 更新时间:2023-12-04 17:41:10 28 4
gpt4 key购买 nike

我创建了一个自定义帖子类型“stm_media_gallery”以及此自定义帖子类型中的三个类别。我想显示与每个帖子关联的类别名称。

<?php $gallery_query = new WP_Query( array('post_type' => 
'stm_media_gallery', 'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post(); ?>
--Display post name and its category name
<?php endif; ?>
<?php endwhile; ?>

最佳答案

你只需要将下面的代码放在循环中:

<div>
<?php
foreach((get_the_category()) as $category){
echo $category->name."<br>";
echo category_description($category);
}
?>
</div>

更新现有代码

    <?php $gallery_query = new WP_Query( 
array('post_type' => 'stm_media_gallery',
'posts_per_page' => -1) );

if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post();

$gallery_category = get_the_category( get_the_ID() );

the_title( '<h3>', '</h3>' );
echo "<br>";
<?php foreach ( $gallery_category as $key => $value) { echo $value->category_nicename; } ?>


<?php endif; ?>
<?php endwhile; ?>

关于php - 在 while 循环中获取当前帖子类别名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54438802/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com