我不太确定你的祖父母页面是如何获得这种风格的,因为你的代码说要从父母那里获取字段。祖父母的后父母应为0,这意味着他们没有父母。也许你的等级制度中我遗漏了一些东西。
条件应在此处起作用:
(假设您已经指定global $post
在模板中):
<div class="<?php
// if this is a top-level post, grab its field
if($post->post_parent == 0) { ?>
// output the field - no need to pass an ID as it is the ID of this post
the_field(\'colour\');
// if this is not a top-level post (child, grandchild, or so forth)
} else {
// get all of this post\'s ancestors: parent, grandparent, and so forth
$ancestors = get_post_ancestors();
// get the last item in the array - the "top" parent (grandparent)
$id = end($ancestors);
// now pull its field
the_field(\'colour\', $id);
} ?>
">
实现相同效果的非ACF方法是将所需的类添加到
<body>
中的类
functions.php
:
<?php // add a filter to the body_class() function
add_filter(\'body_class\', \'wpse_310439\');
function wpse_310429($classes) {
// pull ancestors of the current page/post/cpt
$ancestors = get_post_ancestors();
// if this is a particular page ID, or a child of that ID
if(is_page(\'12345\') || end($ancestors) == \'12345\') {
// add a class to the <body>
$classes[] = \'blue\';
} elseif(is_page(\'54321\') || end($ancestors == \'54321\') {
$classes[] = \'yellow\';
}
// always return $classes so body gets regular classes
return $classes;
} ?>
通过这种方式,您可以在主题文件中设置类,并且还需要使用级联来针对任何需要着色的div(而不是
.blue
您可以使用以下内容
body.blue div.styleme
), 但不需要插件。:)