[サイト作り] [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">

Did you like this? Share it:

パーマリンク

投稿 URL

投稿をリンクする

抜粋付きリンクタグ

関連してるかも

  • 2009年12月1日 -- [WordPress] id や class にカテゴリースラッグ
    body や div の id や class にカテゴリースラッグを付ける bodyに付ける ...
  • 2009年9月28日 -- [WordPress] body idを、投稿者IDに出来るかテストしてみた
    フロント、カテゴリーやアーカイブページなどは無理だが、シングルページ(single.php)、ページ(page.php)は可能。 is_author()で、投稿者のページも出来ないかテストしたもののこちらも無理っぽい。 とりあえず、シングルページだけでも変更できれば、投稿者ごとに背景やレイアウトの変更が出来るので ...