将帖子类别输出为班级 [英] Outputting a posts categories as a class
本文介绍了将帖子类别输出为班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现有的代码输出投资组合类别中的所有帖子,但是我还想向该帖子所涉及的类别填充的每个项目中添加一个类。我尝试使用get_the_category并认为我已经快到了,但无法解决我尚未完全正确的操作:
My existing code outputs all posts in the portfolio category, however I want to also add a class to each item that is populated by the categories the post relates to. I've tried using get_the_category and think i'm nearly there but can't work out what I haven't quite done correct:
<?php query_posts('category_name=portfolio&order=DSC&orderby=ID&posts_per_page=20');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$portfolio_link = get_post_meta($post->ID, 'portfolio_link', true);
$categories = get_the_category($postID);
?>
<li class="<?php echo $categories->cat_name;?>">
<?php if ($portfolio_link) { echo "<a href='$portfolio_link'>"; } ?>
<?php the_post_thumbnail('small'); ?>
<?php if ($portfolio_link) { echo "</a>"; } ?>
</li>
<?php endwhile; endif; wp_reset_query();?>
目前根本没有输出任何类,但也没有显示错误!
At the moment no classes are output at all, but no errors are shown either!
推荐答案
get_the_category()返回一个数组,因此您需要对其进行迭代。
get_the_category() returns an array so you need to iterate over it.
$catNames = array();
foreach($categories as $category) {
$catNames[] = $category->cat_name;
}
$classes = implode(' ', $catNames);
<li class="<?php echo $classes ?>">
这篇关于将帖子类别输出为班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文