php 在 wordpress 循环中显示帖子的类别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14128030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display a post's category within the wordpress loop?
提问by Tom Nolan
Okay, so I have a Wordpress template that I created that only displays posts that have the "workout" category associated with it. Within the loop that displays those, I want the categories of the specific post to be listed.
好的,所以我创建了一个 Wordpress 模板,该模板仅显示与“锻炼”类别相关联的帖子。在显示这些的循环中,我希望列出特定帖子的类别。
I was thinking something like this would work:
我在想这样的事情会起作用:
$id = get_the_ID();
$cats = wp_get_post_categories($id);
But then I do not know how to echo this out on to the screen. Anyone have any idea how I can display the categories of each post within the loop? All of the articles I have looked at have only showed how to display all categories, not display the categories associated with a specific post.
但后来我不知道如何在屏幕上回显这一点。任何人都知道如何在循环中显示每个帖子的类别?我看过的所有文章都只展示了如何显示所有类别,而不是显示与特定帖子相关联的类别。
Here is the loop I have:
这是我的循环:
<div class="query">
<b><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></b>
<?php
$id = get_the_ID();
$cats = wp_get_post_categories($id);
?>
</div>
<?php endwhile; ?>
回答by Joseph Silber
Just echo the name:
只需回显名称:
echo $cats[0]->name;
If you want to output a link, use this:
如果要输出链接,请使用以下命令:
<a href="<?php echo get_category_link($cats[0]->cat_ID); ?>">
<?php echo $cats[0]->name; ?>
</a>
Note:instead of wp_get_post_categories($id), you could just use get_the_category().
注意:代替wp_get_post_categories($id),您可以使用get_the_category().
Update:if you want to display all the categories, just loop through them:
更新:如果要显示所有类别,只需循环遍历它们:
<?php foreach ( $cats as $cat ): ?>
<a href="<?php echo get_category_link($cat->cat_ID); ?>">
<?php echo $cat->name; ?>
</a>
<?php endforeach; ?>
回答by James Koussertari
Thanks Joseph. I have extended your code so that the word 'Category' changes to 'Categories' when there are more than one category. There may be a better way of doing this but I couldn't find it anywhere :)
谢谢约瑟夫。我已经扩展了您的代码,以便当有多个类别时,“类别”一词会更改为“类别”。可能有更好的方法来做到这一点,但我在任何地方都找不到:)
<p>
<?php
$id = get_the_ID();
$cats = get_the_category($id);
echo ( count($cats) == 1 ? 'Category: ' : 'Categories: ');
$c = 0; $n = 0;
$c = count($cats);
foreach ( $cats as $cat ):
$n++; ?>
<a href="<?php echo get_category_link($cat->cat_ID); ?>">
<?php echo $cat->name; echo ( $n > 0 && $n < $c ? ', ' : ''); ?>
</a>
<?php endforeach; ?>
</p>
回答by Muddasir Abbas
Get the post category if you have a custom post_type
如果您有自定义 post_type,则获取帖子类别
<?php
$categories = get_the_terms( $post->ID, 'taxonomy' );
// now you can view your category in array:
// using var_dump( $categories );
// or you can take all with foreach:
foreach( $categories as $category ) {
echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
} ?>
回答by Karadjordje
If anybody else needs help with this you can use this inside posts loop:
如果其他人需要这方面的帮助,您可以在帖子循环中使用它:
<p><?php _e( 'Category: ', 'themename' ); the_category(', '); // Separated by commas ?></p>

