admin管理员组文章数量:1278910
I am still learning but I wasnt able to style a category link. I was able to create the following so only 1 category link is displayed. But I was unsure how I can modify this so I can add a class and style it. Is anyone able to help?
<?php
// category link
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id )
) . '">' . esc_html( $categories[0]->name ) . '</a>';
}
// category link
?>
I am still learning but I wasnt able to style a category link. I was able to create the following so only 1 category link is displayed. But I was unsure how I can modify this so I can add a class and style it. Is anyone able to help?
<?php
// category link
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id )
) . '">' . esc_html( $categories[0]->name ) . '</a>';
}
// category link
?>
Share
Improve this question
asked Sep 23, 2021 at 7:40
Gmad Gmad
1
2
|
1 Answer
Reset to default 0Here is an example of how you can do it with your code, using the category name as the class name:
<?php
// category link
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a class="' .esc_html( $categories[0]->name ) . '" href="' . esc_url( get_category_link( $categories[0]->term_id )
) . '">' . esc_html( $categories[0]->name ) . '</a>';
}
// category link
?>
本文标签: phpStyling a category link
版权声明:本文标题:php - Styling a category link 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304082a2371264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<a href="
. Just add a class to it. – Jacob Peattie Commented Sep 23, 2021 at 7:44