Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active December 11, 2015 03:19
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/4536641 to your computer and use it in GitHub Desktop.
Save s-hiroshi/4536641 to your computer and use it in GitHub Desktop.
WordPress > snippets 基準カテゴリーの子孫カテゴリーリストを出力
<?php
/**
* 子孫カテゴリー表示ウィジェット
* @author Sawai Hiroshi
*/
class DescendantCategoryWidget extends WP_Widget {
/** constructor */
function DescendantCategoryWidget() {
parent::WP_Widget(false, $name = 'DescendantCategoryWidget');
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
if (is_category()) {
global $cat;
$category = get_category($cat);
try {
$hierarchy = new HierarchyCategory($cat);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
echo ('<h2><a href="' . esc_url(get_category_link($category->cat_ID)) . '">' . esc_html($category->name) . '</a></h2>');
echo $hierarchy->getDescendant();
}
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
// nothing
}
/** @see WP_Widget::form */
function form($instance) {
// nothing
}
}
<?php
/**
* 基準カテゴリーをベースに様々なカテゴリーIDの配列を保持する。
*
* @author Sawai Hiroshi
*/
class HierarchyCategory {
// 基準カテゴリーID
private $base_id;
// 出力HTML
private $output = '';
/**
* @constructor
* @throws InvalidArgumentException 引数がカテゴリーIDでない
*/
public function __construct($cat_id) {
$all_cat_ids = get_all_category_ids();
if (false === in_array(intVal($cat_id), $all_cat_ids)) {
throw new InvalidArgumentException('argument is not category id');
};
$this->base_id = $cat_id;
}
/**
* 基準カテゴリーの(孫要素を除く)子カテゴリーID配列を返す
*
* @param $cat_id カテゴリーID
* @throws InvalidArgumentException 引数がカテゴリーIDでない
* @return array $child_ids (孫要素を含まない)子要素のカテゴリーIDの配列(自身は含まない)
*/
private function getChildIds($cat_id) {
$all_cat_ids = get_all_category_ids();
if (false === in_array(intVal($cat_id), $all_cat_ids)) {
throw new InvalidArgumentException('argument is not category id');
};
$descendant_ids = get_term_children($cat_id, 'category');
$child_ids = array();
for ($i = 0; $i < count($descendant_ids); $i++) {
$category = get_category($descendant_ids[$i]);
if ($cat_id == $category->parent) {
array_push($child_ids, $descendant_ids[$i]);
}
}
return $child_ids;
}
/**
* 子孫カテゴリーをHTMLUlElementで出力
* @param $cat_id カテゴリーID
* @return string 出力用HTML
* @throws InvalidArgumentException 引数がカテゴリーIDでない
*/
private function makeDescendantHtml($cat_id) {
$all_cat_ids = get_all_category_ids();
if (false === in_array(intVal($cat_id), $all_cat_ids)) {
throw new InvalidArgumentException('argument is not category id');
}
$child_ids = $this->getChildIds($cat_id);
if (count($child_ids) === 0) {
$this->output .= '<li><a href="' . get_category_link($cat_id) . '">' . get_category($cat_id)->name . '</a></li>';
} else {
$this->output .= '<li><a href="' . get_category_link($cat_id) . '">' . get_category($cat_id)->name . '</a>';
for ($i = 0; $i < count($child_ids); $i++) {
if ($i === 0) {
$this->output .= '<ul>';
}
$this->makeDescendantHtml($child_ids[$i]);
if ($i === count($child_ids) - 1) {
$this->output .= '</li></ul>';
}
}
}
return $this->output;
}
/**
* 基準カテゴリーの子孫カテゴリーリストを出力
* @return string 出力用HTML
*/
public function getDescendant() {
$child_ids = $this->getChildIds($this->base_id);
if (count($child_ids) > 0) {
$this->output .= '<ul>';
for ($i = 0; $i < count($child_ids); $i++) {
$this->makeDescendantHtml($child_ids[$i]);
}
$this->output .= '</ul>';
return $this->output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment