ログイン

[サイト作り] [WordPress] body classを付ける

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">

Sponsored Link
[キャンペーン] パンテーントライアルキットプレゼント

「濃厚トリートメントプログラム(洗い流すタイプ)」トライアルキットが抽選で総計5,000名様に

応募期間:
2010年2月15日~2月26日まで
2010年2月27日~3月18日まで
2010年3月19日~4月2日まで

応募フォーム
パンテーンサイト