admin管理员组文章数量:1122846
I want to change category labels colors on every item in my blog list and also in every single post depending on the category. Also want to change background colors of the items for every category. Does it possible to make this with JS or Jquery? Also, I use theme with ajax pagination and navigation, so I need this function to be triggered every time new item is loaded or single post is opened.
I want to change category labels colors on every item in my blog list and also in every single post depending on the category. Also want to change background colors of the items for every category. Does it possible to make this with JS or Jquery? Also, I use theme with ajax pagination and navigation, so I need this function to be triggered every time new item is loaded or single post is opened.
Share Improve this question asked Jun 27, 2019 at 7:47 Georgi PehlivanovGeorgi Pehlivanov 234 bronze badges 5 |1 Answer
Reset to default 0Here's an easy way to add the appropriate class to each category label.
function custom_the_content( $content ) {
$search = ['<a href="link1">First category name</a> ', '<a href="link2">Second category name</a>'];
$replace = ['<a class="class1" href="link1">First category name</a> ', '<a class="class2" href="link2">Second category name</a> ',];
return str_replace($search, $replace, $content);
}
add_filter('the_content', 'custom_the_content');
Thanks to the use of an array, you can get several strings of characters at once and add a class to each string. The code comes from here: https://dev21.pl/jak-dodac-inny-kolor-do-kazdej-kategorii/#jak-dodac-inny-kolor-do-kazdej-kategorii-caly-kod-do-skopiowania and there you also have an additional explanation of the code, in case something is not understandable
本文标签: categoriesHow to change category labels in different colors
版权声明:本文标题:categories - How to change category labels in different colors? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302262a1931471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<?php foreach((get_the_category()) as $category) { echo $category->slug . '-color'; } ?>
– Georgi Pehlivanov Commented Jun 27, 2019 at 14:04