【问题标题】:ACF Custom location rule to select grandparent pageACF 自定义位置规则以选择祖父母页面
【发布时间】:2015-07-28 08:14:10
【问题描述】:

我正在尝试构建一个自定义位置规则,以便能够选择祖父页面,但没有成功...这是我的目标:

在以下情况下显示此字段组:

Page Grand-Parent -------- 等于 --------- page x

有人吗? 谢谢!

约翰

ACF Documentation 在这个问题上有点差...

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    这就是答案...

    只需将这些行复制到您的 functions.php...

    感谢在 ACF 论坛上提供帮助的 John Huebner...

    add_filter('acf/location/rule_types', 'acf_location_rules_page_grandparent');
    function acf_location_rules_page_grandparent($choices) {
        $choices['Page']['page_grandparent'] = 'Page Grandparent';
        return $choices;
    }
    
    add_filter('acf/location/rule_values/page_grandparent','acf_location_rules_values_page_grandparent');
    function acf_location_rules_values_page_grandparent($choices) {
      // this code is copied directly from 
      // render_location_values() 
      // case "page"
      $groups = acf_get_grouped_posts(array(
        'post_type' => 'page'
      ));
      if (!empty($groups)) {
        foreach(array_keys($groups) as $group_title) {
          $posts = acf_extract_var($groups, $group_title);
          foreach(array_keys($posts) as $post_id) {
            $posts[$post_id] = acf_get_post_title($posts[$post_id]);
          };
          $choices = $posts;
        }
      }
      // end of copy from ACF
      return $choices;
    }
    
    add_filter('acf/location/rule_match/page_grandparent', 'acf_location_rules_match_page_grandparent', 10, 3);
    function acf_location_rules_match_page_grandparent($match, $rule, $options) {
      // this code is with inspiration from
      // acf_location::rule_match_page_parent()
      // with addition of adding grandparent check
      if(!$options['post_id']) {
        return false;
      }
      $post_grandparent = 0;
      $post = get_post($options['post_id']);
      if ($post->post_parent) {
        $parent = get_post($post->post_parent);
        if ($parent->post_parent) {
          $post_grandparent = $parent->post_parent;
        }
      }
      if (isset($options['page_parent']) && $options['page_parent']) {
        $parent = get_post($options['page_parent']);
        if ($parent->post_parent) {
          $post_grandparent = $parent->post_parent;
        }
      }
      if (!$post_grandparent) {
        return false;
      }
      if ($rule['operator'] == "==") {
        $match = ($post_grandparent == $rule['value']);
      } elseif ($rule['operator'] == "!=") {
        $match = ($post_grandparent != $rule['value']);
      }
      return $match;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多