WordPress如何只显示顶级类别
WordPress how to display top level category only
我正在为我的 WordPress 网站设计 category/archives
页面,我一直在尝试写出这样的页面标题:
作品集:婚礼
我一直在使用实现此目的的代码,但是,由于 parent 和 sub-category 在同一个 php
中,尝试使用 [= 单独设置样式很烦人27=](主要针对移动设备)。
<span><?php echo implode( ": ", array_filter( explode( ": ", get_category_parents( $cat, true, ": " ) ) ) ); ?></span>
有什么方法可以实现更像这样的东西吗?
<span class="parent-cat"><?php DISPLAY PARENT NAME ; ?></span>
<span class="child-cat"><?php printf( esc_html__( '%s', 'myTheme' ), single_cat_title( '', false ) ); ?></span>
谢谢!
目前您正在使用 get_category_parents
检索 parent 类别。你要求的只是两个 parent 类别,但是如果有两个以上的 parent 类别怎么办?
如果你想单独获得 parents,你可以使用 get_ancestors
功能,这给你更多的灵活性。它将为您提供所有 parents id,您可以在其上设置 foreach
循环并分别输出它们。如果只有一个 parent,那么这正是您要找的。
if (is_category()) {
$query_obj = get_queried_object();
$query_obj_parents_ids = get_ancestors($query_obj->term_id, 'category');
if ($query_obj_parents_ids) {
foreach ($query_obj_parents_ids as $query_obj_parent_id) { ?>
<span class="parent-cat-<?php echo $query_obj_parent_id ?>"><?php printf(esc_html__('%s', 'myTheme'), get_the_category_by_ID($query_obj_parent_id)); ?></span>
<?php
}
}
}
因此,如果您的类别有 3 parent 秒,它将输出:
<span class="parent-cat-11">test category parent 03</span>
<span class="parent-cat-8">test category parent 02</span>
<span class="parent-cat-2">test category parent 01</span>
最接近的parent将首先输出。
另请注意,parent-cat-{$parent-id}
将是您的 class 的名称,它允许您将 parent 定位为 css
and/or javascript
.
请注意,我还使用了 get_the_category_by_ID
函数,它将根据类别 ID 为您提供类别名称。
此外,如果您需要同一页面的类别名称(即页面标题),而不是 parent 的名称,那么您可以使用 echo $query_obj->name;
。
这是您在问题中要求的示例:
if (is_category()) {
$query_obj = get_queried_object();
$query_obj_parents_ids = get_ancestors($query_obj->term_id, 'category');
if ($query_obj_parents_ids) {
foreach ($query_obj_parents_ids as $query_obj_parent_id) {
?>
<span class="parent-cat"><?php echo get_the_category_by_ID($query_obj_parent_id) ?></span>
<?php
}
}
?>
<span class="child-cat"><?php printf(esc_html__('%s', 'myTheme'), $query_obj->name); ?></span>
<?php
}
这将输出:
<span class="parent-cat">Portfolio</span>
<span class="child-cat">Weddings</span>
我正在为我的 WordPress 网站设计 category/archives
页面,我一直在尝试写出这样的页面标题:
作品集:婚礼
我一直在使用实现此目的的代码,但是,由于 parent 和 sub-category 在同一个 php
中,尝试使用 [= 单独设置样式很烦人27=](主要针对移动设备)。
<span><?php echo implode( ": ", array_filter( explode( ": ", get_category_parents( $cat, true, ": " ) ) ) ); ?></span>
有什么方法可以实现更像这样的东西吗?
<span class="parent-cat"><?php DISPLAY PARENT NAME ; ?></span>
<span class="child-cat"><?php printf( esc_html__( '%s', 'myTheme' ), single_cat_title( '', false ) ); ?></span>
谢谢!
目前您正在使用 get_category_parents
检索 parent 类别。你要求的只是两个 parent 类别,但是如果有两个以上的 parent 类别怎么办?
如果你想单独获得 parents,你可以使用 get_ancestors
功能,这给你更多的灵活性。它将为您提供所有 parents id,您可以在其上设置 foreach
循环并分别输出它们。如果只有一个 parent,那么这正是您要找的。
if (is_category()) {
$query_obj = get_queried_object();
$query_obj_parents_ids = get_ancestors($query_obj->term_id, 'category');
if ($query_obj_parents_ids) {
foreach ($query_obj_parents_ids as $query_obj_parent_id) { ?>
<span class="parent-cat-<?php echo $query_obj_parent_id ?>"><?php printf(esc_html__('%s', 'myTheme'), get_the_category_by_ID($query_obj_parent_id)); ?></span>
<?php
}
}
}
因此,如果您的类别有 3 parent 秒,它将输出:
<span class="parent-cat-11">test category parent 03</span>
<span class="parent-cat-8">test category parent 02</span>
<span class="parent-cat-2">test category parent 01</span>
最接近的parent将首先输出。
另请注意,parent-cat-{$parent-id}
将是您的 class 的名称,它允许您将 parent 定位为 css
and/or javascript
.
请注意,我还使用了 get_the_category_by_ID
函数,它将根据类别 ID 为您提供类别名称。
此外,如果您需要同一页面的类别名称(即页面标题),而不是 parent 的名称,那么您可以使用 echo $query_obj->name;
。
这是您在问题中要求的示例:
if (is_category()) {
$query_obj = get_queried_object();
$query_obj_parents_ids = get_ancestors($query_obj->term_id, 'category');
if ($query_obj_parents_ids) {
foreach ($query_obj_parents_ids as $query_obj_parent_id) {
?>
<span class="parent-cat"><?php echo get_the_category_by_ID($query_obj_parent_id) ?></span>
<?php
}
}
?>
<span class="child-cat"><?php printf(esc_html__('%s', 'myTheme'), $query_obj->name); ?></span>
<?php
}
这将输出:
<span class="parent-cat">Portfolio</span>
<span class="child-cat">Weddings</span>