在function.php添加代码:
/** * 获取WordPress所有分类名称和ID * https://www.lrmb.net */ function show_category(){ global $wpdb; $request = "SELECT $wpdb->terms.term_id, name FROM $wpdb->terms "; $request .= " LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id "; $request .= " WHERE $wpdb->term_taxonomy.taxonomy = 'category' "; $request .= " ORDER BY term_id asc"; $categorys = $wpdb->get_results($request); foreach ($categorys as $category) { //调用菜单 $output = ''.$category->name."(".$category->term_id.')'; echo $output; } }
在想要输出分类的位置添加如下代码:
<?php show_category(); ?>
分类输出演示效果: