来自祖先页面的高级自定义字段数据
问题描述:
我想在父页下的两个级别上的子页面上提供ACF数据。我有一个解决方案,使其可用于儿童页面:来自祖先页面的高级自定义字段数据
if ($post->post_parent) {
$headingFont = get_field('community_heading_font', $post->post_parent);
$bodyFont = get_field('community_body_font', $post->post_parent);
$primaryColor = get_field('community_primary', $post->post_parent);
$secondaryColor = get_field('community_secondary', $post->post_parent);
$fifteenSecondaryColor = get_field('community_fifteen_secondary', $post->post_parent);
$tertiaryColor = get_field('community_tertiary', $post->post_parent);
}
但是,一旦我们是更深的级别,此信息不可用。也就是说,ACF字段'community_heading_font'
不可用于最初为该字段提供数据的页面的孙子。
我试过post->post_parent->post_parent
,我也试着对一个变量使用post->post_parent
:
$parentPage = $post->post_parent;
$grandparentPage = $parentPage->post_parent
答
要得到$ grandparentPage ID用于您的ACF的功能,使用wp_get_post_parent_id()函数。
$grandparentPage = wp_get_post_parent_id($post->post_parent);
$headingFont = get_field('community_heading_font', $grandparentPage);
https://codex.wordpress.org/Function_Reference/wp_get_post_parent_id