body class を、付けれるハック
対応バージョン wp2.7以降(2.8でテスト)
参考:英語サイト
Extending the WordPress body_class function with the top level parent ID
function.php に、次のコードを入れる
<?php
add_filter('body_class','top_level_parent_id_
body_class');
function top_level_parent_id_body_class($classes) {
global $wpdb, $post;
if (is_page()) {
if ($post->post_parent) {
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
$classes[] = 'top-level-parent-pageid-' . $parent;
}
return $classes;
}
?>
このようになる。
トップページ
<body class="home blog">
カテゴリー
<body class="archive category category-1">
アーカイブ
<body class="archive date">
シングルページ
<body class="single postid-13">
ページ
<body class="page page-id-2 parent-pageid-0 page-template- top-level-parent-pageid-2">
