【问题标题】:wordpress php add post category as class to post itemwordpress php将帖子类别添加为发布项目的类
【发布时间】:2013-12-16 21:40:52
【问题描述】:
我在一个页面上显示我所有帖子的特色图片(然后图片链接到单个帖子)。我想为同一类别的每个帖子提供相同的类(这将是类别 slug 的名称),例如,如果帖子属于名为“cat1”的类别,我希望包含帖子的 div有“cat1”类等等。
到目前为止,我有这个:
<article class="<?php get_the_category($post->ID)?>">
但这只是返回空白,请有人告诉我我在这里缺少什么。
【问题讨论】:
标签:
php
wordpress
class
categories
【解决方案1】:
Codex 是你的朋友:
// add category nicenames in body and post class
function so20621481_category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes[] = $category->category_nicename;
return $classes;
}
add_filter('post_class', 'so20621481_category_id_class');
注意:为此,您的文章元素应包含post_class 模板标签:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>