php - 在 wordpress 中动态获取分类的所有类别名称
问题描述
我必须为朋友做一个项目,我需要知道是否可以在 wordpress 中动态检索分类的所有类别名称。如果可以,我该怎么办?
解决方案
是的,你可以做到!首先在function.php文件末尾添加以下代码:
function current_cat() {
global $post;
if ( is_page() && $post->post_parent )
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' . '&depth=1' );
else
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' . '&depth=1' );
if ( $childpages ) {
$string = '<ul>' . $childpages . '</ul>';
}
return $string;
}
add_shortcode('currentcat', 'current_cat');
然后,您几乎可以通过调用短代码名称在任何地方使用它:
[currentcat]
推荐阅读
- objective-c - NSUrlSession 代理请求失败,错误代码为 310 (kCFErrorHTTPSProxyConnectionFailure)
- firebase - 运行 Gradle 时出错 - gradlew.bat 异常退出(添加 firebase 后出错)
- javascript - 如何在赛普拉斯自动化工具中处理定位器的动态 ID
- php - 找不到 url 在 url 上发送编码参数
- javascript - 有人可以向我解释一下这个功能的作用是什么
- java - Java Inheritance - Fly(程序)运行时打印什么?
- jquery - wordpress 中的 jQuery onscroll 无法正常工作
- python - 如果边界超过,将一个 numpy 数组复制到另一个数组的一部分
- neural-network - 在 Pytorch 中正确的数据加载、拆分和扩充
- ios - swagger-codegen swift4 可编码协议