简体   繁体   English

WordPress - 包括类别slug中的子类别

[英]WordPress - include sub-categories in category slug

The following script outputs an icon image depending on the category slug, I would like to add in child categories of the 'Service' category - so that these will show the icon too. 以下脚本根据类别slug输出图标图像,我想在“服务”类别的子类别中添加 - 这样它们也会显示图标。 Would this ideally be in the form of an array? 这理想情况下是阵列的形式吗?

<?php
   $category = get_the_category(); 
   $category_slug = $category[0]->slug;

   if($category_slug == 'service') {
?>
<img src="<?php echo get_template_directory_uri(); ?>/img/icon/service.png" alt="service"><span>Service</span>

<?php
}
?>

Many thanks for your help. 非常感谢您的帮助。

Yes, you can get that data and that will be array. 是的,您可以获得该数据,这将是数组。 here is a piece of code that will elaborate a bit. 这是一段代码,稍后会详细说明。

<?php
$category = get_the_category(); 
$category_slug = $category[0]->slug;

if($category_slug == 'service') {
$category_id = $category->term_id;
?>
<img src="<?php echo get_template_directory_uri(); ?>/img/icon/service.png"    alt="service"><span>Service</span>

<?php
$children = get_term_children($category_id, '**your taxonomy name here**');
foreach($children as $child) {
<img src="<?php echo get_template_directory_uri(); ?>/img/icon/service.png"    alt="service">
}
}
?>

Now, paste this code in place of your code and replace your taxonomy name here with your taxonomy name. 现在,粘贴此代码代替您的代码,并在此处使用您的分类名称替换的分类名称。 Now, your sub categories will be taking your desired image. 现在,您的子类别将采用您想要的图像。 For further reading please visit codex page 如需进一步阅读, 请访问codex页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM