エントリの末端ジャンルを返す便利関数

この関数はループ中で使うことを前提としてる。

function cat_last_child(){
	$cats = get_the_category();
	$current = '';
	foreach($cats as $cat){
		if(!$current || cat_is_ancestor_of($current, $cat)) {
			$current = $cat;
		}
	}
	return $current;
}

上記を、function.phpへ追加する。

オブジェクトとして返しているので、テンプレ側ではecho などで出力する

<?php
	$my_cat = cat_last_child();
	echo $my_cat->name; // 末端のカテゴリ名
	echo $my_cat->cat_ID; // 末端のカテゴリID
?>

Aカテゴリーが、Bカテゴリーの祖先にあたるかどうかを返してくれるcat_is_ancestor_ofという関数
http://ja.forums.wordpress.org/topic/1290?replies=5 より。

参考URL: http://ja.forums.wordpress.org/topic/3122?replies=4