WordPress获取网站所有分类目录名称、ID号、链接

在网站开发中,常常需要获取网站所有分类的名称和ID号,来实现调用相应的数据。在wordpress建站视频教程中我们介绍了怎么创建分类目录,下面学做网站论坛介绍一下WordPress获取网站所有分类目录名称、ID号、链接

方法一:通过SQL语句进行获取

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;
    }

方法二:通过程序语句获取

$args=array(
  'orderby' => 'name',
  'include'=> '1,3,2',//需要显示的分类ID
  'order' => 'ASC'
  );
$categories=get_categories($args);
  foreach($categories as $category) {
   echo "<a href=get_category_link( $category->term_id ) title=$category->name > $category->name </a>";
    }

以上代码请在后台观看才准确