【问题标题】:Advanced Custom Fields data from ancestor page来自祖先页面的高级自定义字段数据
【发布时间】:2017-07-20 06:19:35
【问题描述】:

我正在尝试使 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

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    要获取用于 ACF 函数的 $grandparentPage ID,请使用 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

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 1970-01-01
      • 2016-06-28
      • 2021-02-18
      • 2016-01-15
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2013-03-24
      相关资源
      最近更新 更多