WordPress - 在类别 slug 中包含子类别
WordPress - include sub-categories in category slug
以下脚本根据类别 slug 输出图标图像,我想添加 'Service' 类别的子类别 - 这样它们也会显示图标。这最好是数组的形式吗?
<?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
}
?>
非常感谢您的帮助。
是的,您可以获取该数据,这将是数组。这是一段代码,将详细说明。
<?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">
}
}
?>
现在,粘贴此代码代替您的代码,并将此处的分类名称替换为您的分类名称。现在,您的子类别将采用您想要的图像。进一步阅读 please visit codex page
以下脚本根据类别 slug 输出图标图像,我想添加 'Service' 类别的子类别 - 这样它们也会显示图标。这最好是数组的形式吗?
<?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
}
?>
非常感谢您的帮助。
是的,您可以获取该数据,这将是数组。这是一段代码,将详细说明。
<?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">
}
}
?>
现在,粘贴此代码代替您的代码,并将此处的分类名称替换为您的分类名称。现在,您的子类别将采用您想要的图像。进一步阅读 please visit codex page