WordPress数据库语句,查询分类信息名和id

作者: JONE 分类: PHPer 发布时间: 2017-09-13 23:05
      <?php //获取所有站点分类id
         function Bing_show_category()
         {
             //$wpdb数据库实例化的对象
             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 . "=(<b>" . $category->term_id . '</b>)</span>&nbsp;&nbsp;';
                 echo $output;
             }
         }
      ?>

执行函数Bing_show_category() 为输出如下格式的分类信息
未分类=(1)  分类一=(7) 未分类二=(10)

发表评论

电子邮件地址不会被公开。 必填项已用*标注