首页 > 解决方案 > 在 wordpress 中动态获取分类的所有类别名称

问题描述

我必须为朋友做一个项目,我需要知道是否可以在 wordpress 中动态检索分类的所有类别名称。如果可以,我该怎么办?

标签: phpwordpressadd

解决方案


是的,你可以做到!首先在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]

推荐阅读