2013-12-16 58 views
0

我在一个页面上显示我所有帖子的特色图片(图片然后链接到各个帖子)。我想给每个帖子从同一类别的同一类(这将是类别slug的名称),例如,如果一个职位是在一个名为“cat1”的类别,我希望包含帖子的div有班级“cat1”等。wordpress php添加帖子类别作为类发布项目

到目前为止,我有这样的:

<article class="<?php get_the_category($post->ID)?>"> 

但这只是返回空白,可有人告诉我,我很想念这里请。

回答

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(); ?>> 
相关问题