カテゴリーページで現在・親・子カテゴリーのIDを取得する

Pocket

カテゴリーページでカテゴリー現在・親・子カテゴリーのIDを配列で取得する


functions.php

/**
 * 表示カテゴリーをベースにカテゴリーIDのリストを保持する。
 *     1 現在のカテゴリーID。
 *     2 親カテゴリー(現在カテゴリーは含まない)
 *     3 子カテゴリー(現在カテゴリーを含まない)
 *
 * @author Sawai Hiroshi
 * @version 1.0
 */

/*
 * getChildTreeは下記の記事のスクリプトを利用
 * 配列をツリー構造にする-PHPアルゴリズム
 * http://www.php-z.net/c10/n84.html
 */
class Category {
    // 現在のカテゴリーID(整数)
    private $current;
    // 現在のカテゴリーの親カテゴリーID(現在のカテゴリーIDを含む)
    private $parentList = array();
    // 現在のカテゴリーの子カテゴリーID(現在のカテゴリーIDを含む)
    private $childList = array(); 

    /**
     * 親カテゴリーリストにカテゴリーIDを追加する。
     * 
     * @param {Int} $cat_id カテゴリーID
     */
    private function addParentList($cat_id) {
        $category = get_category($cat_id);
        if ($category->parent != 0) {
            array_push($this->parentList, $category->parent);
            // recursive
            $this->addParentList($category->parent);
        }
    }
    /**
     * 現在のカテゴリーの親カテゴリーをparentListへ追加する。
     */
    private function setParentList() {
        if (!is_category()) {
            throw new Exception('Not Category');
        }
        // カテゴリーID(整数)
        global $cat;
        $this->addParentList($cat);        
    }
    /**
     * 表示カテゴリーの全ての親カテゴリーIDをリストもつ配列を取得。
     * 現在のカテゴリー含む。
     *
     * グシングルページはグローバル変数$catを取得できない(中身が空)。
     * 
     * @return カテゴリーIDの配列
     */
    public function getParentList() {
        $this->setParentList();
        return $this->parentList;
    }
    /**
     * 現在のカテゴリー
     */
    public function getCurrent() {
        if (!is_category()) {
            throw new Exception('can use only in Category');
        }
        // カテゴリーID(整数)
        global $cat;
        return $cat; 
    }
    /**
     * 子カテゴリー
     */
    private function setChildList() {
        global $cat;
        $childs = get_term_children($cat, 'category'); 
        foreach ($childs as $child) {
            array_push($this->childList, $child);
        }
    }
    /**
     * 子カテゴリー取得
     */
     public function getChildList() {
         if (!is_category()) {
            throw new Exception('can use only in Category');
         }
         $this->setChildList();
         return $this->childList;
     }
     /**
      * 子カテゴリーをツリーで取得
      */
     public function getChildTree() {
	    global $cat;
	
        $category = get_categories($cat);


        $index = array();
        $tree = array();

        foreach ($category as $value) {
            $arr = get_object_vars($value);

            for ($i = 0; $i < count($arr); $i++) {
                $key = key($arr);
                if (($key !== 'term_id') && ($key !== 'parent') && ($key !== 'child')) {
                    unset($arr[$key]);
                    $i--;
                } else {
                    next($arr);
                }
            }
            $id     = $arr['term_id'];
            $parent = $arr['parent'];

            if (isset($index[$id])) {
                $arr['child'] = $index[$id]['child'];
                $index[$id] = $arr;
            } else {
                $index[$id] = $arr;	
            }

            if ($parent == $cat) {
                $tree[] =& $index[$id];
            } else {
                $index[$parent]['child'][] =& $index[$id];
            }
        }

        return $tree;
    }
}
[/php]


category.php

[php]
$catObj = new Category($post->ID);
$parentList = $catObj->getParentList();
$childList = $catObj->getChildList();
$childTree = $catObj->getChildTree();

» [Gist](https://gist.github.com/4521661)

get_categoriesとget_term_childrenのカテゴリーIDの順序が異なる。

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。