暇人の英語雑記ブログ

WordPress の get_the_category で親、子、孫の階層表示をする関数を作成!

2019.04.052018.08.16WordPress
sitemap-image

WordPress の投稿のカテゴリーを表示するには get_the_category() が多用されますが、この関数は階層取得ができません。

カテゴリーを親、子、孫といった方法で設定が可能なのに、そのような並びで表示できないのは大問題ですね。

categories-simple-hierarchy

ちなみに以上のような単純な階層構造なら以下のコードで事足ります。おそらく。たぶん。知らん(投げやり)。

$categories = get_the_category();

uasort( $categories, function( $val1,  $val2 ) {
	if ( $val1->parent === $val2->parent ) {
		return ( $val1->term_id < $val2->term_id ) ? -1 : 1;
	}
	return ( $val1->parent < $val2->parent ) ? -1 : 1;
} );

以上が正しく動作するかどうかは、自身でテストしてください。何となくの思い付きで書いたものなので。。

categories-hierarchy-example

私が解決したいのは、このような階層を保持したカテゴリーが複数設定されている場合の対応です。

categories-hierarchy-result

これを以上のような順番で表示したいのです。

補足
このような無茶苦茶なカテゴリーの設定はするべきではありません。

あくまでも例です。

ちなみに私が開発した WordPress無料テーマ「SCRATCH」で実際に使用しているコードです。

/**
 * Order by term_order
 */
function categories_term_order( $categories ) {
	$category_tree = [];
	$branches = [];

	/*
	 * Here $categories has a list of items in random order like this
	 * This is just an example ok. This kind of category structure should never be made
	 *
	 *	array(
	 * 		0 => {
	 *			'term_id' => 26,
	 * 			'parent' => 4,
	 * 		},
	 * 		1 => {
	 *			'term_id' => 39,
	 * 			'parent' => 2,
	 * 		},
	 * 		2 => {
	 *			'term_id' => 45,
	 * 			'parent' => 25,
	 * 		},
	 * 		3 => {
	 *			'term_id' => 4,
	 * 			'parent' => 25,
	 * 		},
	 * 		4 => {
	 *			'term_id' => 2,
	 * 			'parent' => 0,
	 * 		},
	 * 		5 => {
	 *			'term_id' => 53,
	 * 			'parent' => 25,
	 * 		},
	 * 		6 => {
	 *			'term_id' => 25,
	 * 			'parent' => 0,
	 * 		}
	 *	)
	 *
	 *	Let's go!
	 */

	foreach( $categories as $cat ) {
		if ( 0 === $cat->category_parent ) {
			$category_tree[ $cat->term_id ] = [];
		} else {
			$branches[ $cat->category_parent ][ $cat->term_id ] = '';
		}
	}

	/*
	 * Now it looks like this
	 *
	 *	$category_tree = (
	 *		2 => array(),
	 *		25 => array(),
	 *	)
	 *	$branches = (
	 *		4 => array(
	 *			26 => '',
	 *		),
	 *		2 => array(
	 *			39 => '',
	 *		),
	 *		25 => array(
	 *			45 => '',
	 *			4 => '',
	 *			53 => '',
	 *		),
	 *	)
	 *
	 * This means 2 and 25 are the oldest ancestors
	 * 26 is a child of 4
	 * 39 is a child of 2
	 * 45 is a child of 25
	 * 4 is a child of 25
	 * 53 is a child of 25
	 *
	 * Let's insert each branch into $category_tree!
	 */

	if ( count( $branches ) ) {
		foreach( $branches as $foundation => $branch ) {
			foreach( $branches as $key => $val ) {
				if ( array_key_exists( $foundation, $val ) ) {
					$branches[ $key ][ $foundation ] = $branches[ $foundation ];
					break 1;
				}
			}
		}
		foreach ( $branches as $foundation => $branch ) {
			if ( isset( $category_tree[ $foundation ] ) ) {
				$category_tree[ $foundation ] = $branch;
			} else {

				/*
				 * This else is when $category_tree and $branches look like these
				 *
				 *	$category_tree = (
				 *		2 => array(),
				 *		25 => array(),
				 *	)
				 *	$branches = (
				 *		4 => array(
				 *			26 => '',
				 *		),
				 *	)
				 *
				 *	This means category 2, 25 and 26 are set but not 4. So 26 is like a lost child
				 *
				 *	The code below makes $category_tree like this
				 *
				  *	$category_tree = (
				 *		2 => array(),
				 *		25 => array(),
				 *		26 => array(),
				 *	)
				 */

				$category_tree += [ key( $branch ) => [] ];
			}
		}
	}

	/*
	 * Now $category_tree looks like this
	 *	array(
	 *		2 => array(
	 * 			39 => '',
	 * 		),
	 * 		25 => array(
	 * 			45 => '',
	 * 			4 => array(
	 * 				26 => '',
	 * 			),
	 * 			53 => '',
	 * 		),
	 * 	)
	 *
	 * Let's flatten it!
	 */

	// multidimensional array flatten
	$array_flatten = function ( $array ) use ( &$array_flatten ) {
		$return = [];

		foreach( $array as $key => $val ) {
			$return[] = $key;

			if ( is_array( $array[ $key ] ) ) {
				$return = array_merge( $return, $array_flatten( $array[ $key ] ) );
			}
		}
		return $return;
	};
	$category_tree = $array_flatten( $category_tree );

	// $category_tree += [ key( $branch ) => [] ]; might make a redundant element in an array
	$category_tree = array_unique( $category_tree );

	/*
	 * Now $category_tree looks like this
	 *	array(
	 *		0 => 2,
	 *		1 => 39,
	 *		2 => 25,
	 *		3 => 45,
	 *		4 => 4,
 	 *		5 => 26,
 	 *		6 => 53,
	 *	)
	 *
	 * Let's insert category data into where it should be
	 * Take it easy
	 */

	foreach ( $category_tree as $key => $cat ) {
		foreach ( $categories as $k => $c ) {
			if ( $cat === $c->term_id ) {
				$category_tree[ $key ] = $c;
			}
		}
	}
	return $category_tree;
}

以下のように使用してください。

categories_term_order( get_the_category() );

Category Order and Taxonomy Terms Order というカテゴリーの順序を変更する有名なプラグインがありますが、以上のコードで不具合などは発生しないのでご安心ください。

ちなみに以下のツイートは、このファンクションを記述した直後に投稿したものです。

本当に勘弁してほしいですね。このようなコードは WordPressコアのほうで実装するべきだと思います。

そうすれば、このようなものも書かなくて済みます。

これよりシンプルで美しい方法を知っている方は、コメント欄で教えてください。

author