wordpress - 如何在所有页面上显示所有子类别
问题描述
我正在尝试显示包含当前父类别的所有子类别的菜单,但不显示父类别。看起来很简单,但我无法删除父名称,或在单个帖子中显示子类别。
这是我目前正在使用的代码
<?php if (is_front_page() or is_single() ) { ?>
<?php } else { ?>
<?php
if (is_category() ) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&use_desc_for_title=1');
else
$this_category = wp_list_categories('orderby=id&depth=1&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
<?php } ?>
解决方案
您可以在要显示子类别的位置使用以下代码
$args = array('child_of' => $this_category->cat_ID);
$categories = get_categories( $args );
echo "<ul>";
foreach($categories as $category) {
echo "<li>".$category->name."</li>";
}
echo "</ul>"
经过测试,效果很好。
推荐阅读
- r - 如何在r中将一列分成两列
- java - 无法在春季启动测试中模拟 persistenceContext
- rpm - 如何指示 yum 从特定 repo 安装特定软件包 (rpm)
- javascript - 无法获取输入框的值
- electron - tray icon doesn't close immediately on app.quit in electron
- powershell - 无法将参数“替换”绑定到目标
- python - python文件显示AttributeError:模块'http'没有属性'client'
- javascript - 正则表达式只允许数字,一个'$'在第一位和一个小数 - javascript
- reactjs - 反应路由器不重新加载旧组件
- intellij-idea - 在 Intellij Idea 的滚动条中显示代码