Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active August 4, 2019 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/ed2f3e80d66fbb9a96e2 to your computer and use it in GitHub Desktop.
Save s-hiroshi/ed2f3e80d66fbb9a96e2 to your computer and use it in GitHub Desktop.
WordPressでカテゴリーツリーを取得するサンプルです。
<?php
ini_set( 'xdebug.var_display_max_children', - 1 );
ini_set( 'xdebug.var_display_max_data', - 1 );
ini_set( 'xdebug.var_display_max_depth', - 1 );
$records = CategoriesTree::get_records_for_category_tree( 起点となるカテゴリーID, array() );
$data = CategoriesTree::set_records_for_category_tree( $records );
$tree = CategoriesTree::get_category_tree( $data[0], array( $data[1][0] ) );
var_dump( $tree );
?>
<?php
class CategoriesTree {
/**
* 子カテゴリー取得
*
* 渡されたカテゴリーの子カテゴリー((孫カテゴリーを除く)の配列を返します。
* すべての子孫カテゴリーはget_term_children()で取得できます。
*
* @param integer $cat_id 起点のカテゴリーIDです。
* @param string $type 戻り値の形式を指定します(id | slug)。
*
* @return mixed(boolean|array) (孫カテゴリーを含まない)子カテゴリーIDの配列(自身は含まない)を返します。
* 引数がカテゴリーIDでないときはfalseを返します。
*/
public static function get_children_by_id( $cat_id, $type = 'id' ) {
// 引数がカテゴリーIDか検査
if ( false === self::is_category_id( $cat_id ) ) {
return false;
}
// 子孫カテゴリーID
$descendants_id = get_term_children( $cat_id, 'category' );
// 子カテゴリーIDを抽出
$children = array();
for ( $i = 0; $i < count( $descendants_id ); $i ++ ) {
$category = get_category( $descendants_id[ $i ] );
// 孫カテゴリーでないことを保証
if ( ! empty( $category ) && $cat_id === $category->parent ) {
if ( 'id' === $type ) {
array_push( $children, $descendants_id[ $i ] );
}
if ( 'slug' === $type ) {
$category = get_category( $descendants_id[ $i ] );
array_push( $children, $category->slug );
}
}
}
return $children;
}
/**
* カテゴリーツリー用レコード作成
*
* @param integer $cat_id 起点となるカテゴリーIDです。
* @param array $record カテゴリー情報を格納した配列です。
*
* @return mixed カテゴリー情報を格納した配列です。
*/
public static function get_records_for_category_tree( $cat_id, $record ) {
$children_id = self::get_children_by_id( $cat_id );
$cat = get_term( $cat_id, 'category' );
array_push(
$record,
array(
'id' => $cat_id,
'parent_id' => $cat->parent,
'name' => $cat->name,
)
);
for ( $i = 0; $i < count( $children_id ); $i ++ ) {
$record = self::get_records_for_category_tree( $children_id[ $i ], $record );
}
return $record;
}
/**
* カテゴリーツリー用配列設定
*
* @param $cat_id
* @param $record
*
* @return mixed
*/
public static function set_records_for_category_tree( $records ) {
$new = array();
foreach ( $records as $record ) {
$new[ $record['parent_id'] ][] = $record;
}
return [
$new,
$records,
];
}
/**
* 配列からカテゴリーの木構造作成
*
* 下記記事のコードをそのまま利用しています。
* https://teratail.com/questions/4518
*
* @link https://teratail.com/questions/4518
*
* @param array $list
* @param array $parent
*
* @return array
*/
public static function get_category_tree( &$list, $parent ) {
$tree = array();
foreach ( $parent as $key => $value ) {
if ( isset( $list[ $value['id'] ] ) ) {
$value['children'] = self::get_category_tree( $list, $list[ $value['id'] ] );
}
$tree[] = $value;
}
return $tree;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment