admin管理员组文章数量:1310306
I am displaying the categories of post through the_category ();
, and I would like to customize the text, such as changing the color, font-size...
This is my code:
<span class="post_info"><?php the_category (', '); ?> </span>
This code only prints the <a>
tag, without class and without ID.
I looked in the codex of the_category
and get_the_category
something about the addition of classes, but found nothing on.
So, how I can add certain class
to this element?
How I can add a class to display a category?
---------------------------------- UPDATE ------------------------------
This code makes that I want:
function add_class_to_category( $thelist, $separator, $parents){
$class_to_add = 'custom-slug';
return str_replace('<a href="', '<a class="'. $class_to_add. '" href="', $thelist);
}
add_filter('the_category', __NAMESPACE__ . '\\add_class_to_category',10,3);
I am displaying the categories of post through the_category ();
, and I would like to customize the text, such as changing the color, font-size...
This is my code:
<span class="post_info"><?php the_category (', '); ?> </span>
This code only prints the <a>
tag, without class and without ID.
I looked in the codex of the_category
and get_the_category
something about the addition of classes, but found nothing on.
So, how I can add certain class
to this element?
How I can add a class to display a category?
---------------------------------- UPDATE ------------------------------
This code makes that I want:
function add_class_to_category( $thelist, $separator, $parents){
$class_to_add = 'custom-slug';
return str_replace('<a href="', '<a class="'. $class_to_add. '" href="', $thelist);
}
add_filter('the_category', __NAMESPACE__ . '\\add_class_to_category',10,3);
Share
Improve this question
edited Aug 3, 2016 at 1:15
Zkk
asked Aug 2, 2016 at 0:19
ZkkZkk
1751 gold badge4 silver badges14 bronze badges
1 Answer
Reset to default 1Instead of using the_category, I would just build the category links manually and add the category slug as the class name. Something like this:
<span class="post_into">
<?php
$thelist = '';
$i = 0;
foreach( get_the_category() as $category ) {
if ( 0 < $i ) $thelist .= ', ';
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" class="' . $category->slug . '">' . $category->name.'</a>';
$i++;
}
echo $thelist;
?>
</span>
本文标签: categoriesHow can I add a class to displays a category
版权声明:本文标题:categories - How can I add a class to displays a category? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741830443a2399900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论