$wpdb获取WordPress所有分类名字和ID

[pcsh lang="php" tab_size="4" message="" hl_lines="" provider="manual"]

/**
 * 获取WordPress所有分类名字和ID
 */
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 = '<span>'.$category->name."(<em>".$category->term_id.'</em>)</span>';
        echo $output;
    }
}

[/pcsh]

其中使用‘->’获取表名,使用‘.’点号获取列名,使用‘,’逗号分割不同的列名

 

 

相关文章

此处评论已关闭