dongsui0929 2017-03-28 09:47
浏览 33
已采纳

自定义字段未显示在自定义帖子类型帖子中

I have a custom post type named "Designer" Each posts will be using different unique Advanced Custom Fields as each posts has unique templates.With the below code I am able to give rules for each posts in Designer post type and save but the custom fields are not displaying on post edit pages on backend. Normally this code should ork but no idea what happend to the code

Please Help.

add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
    $choices['Custom Post types']['cpt_parent'] = 'Custom post type parent';

    return $choices;
}
add_filter('acf/location/rule_values/cpt_parent',    'acf_location_rules_values_cpt_parent');
function acf_location_rules_values_cpt_parent( $choices )
{
    $args = array(
        'hierarchical' => true,
        '_builtin' => false
    );
    $posttypes = get_post_types( $args );

    if( $posttypes )
    {
        foreach( $posttypes as $posttype ):

            if( $posttype != 'acf' ):
                $args = array(
                'post_type' => 'designer',
                'posts_per_page' => -1,
                'post_status' => 'publish'
                );
                $customposts = get_posts( $args );  
                if ( $customposts  ) {
                    foreach( $customposts as $custompost ){
                        $choices[ $custompost->ID] = $custompost->post_title;
                    }
                }
            endif;
        endforeach;
    }

    return $choices;
}

//MATCH THE RULE
add_filter('acf/location/rule_match/cpt_parent',              'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
    global $post;
    $selected_post = (int) $rule['value'];

    // post parent
    $post_parent = $post->post_parent;
    if( $options['page_parent'] ) {

        $post_parent = $options['page_parent'];

    }

    if ($rule['operator'] == "=="){
        $match = ( $post_parent == $selected_post );
    }
    elseif ($rule['operator'] != "!="){
        $match = ( $post_parent != $selected_post );
    }

    return $match;
}

enter image description here

  • 写回答

2条回答 默认 最新

  • dongmi5607 2017-04-04 13:06
    关注

    Your Artist Collection field group is set to only appear on one post, the post Designer Post 1 which is a Designer Post type.

    I don't understand what all the code is for? Just create a different field group for each post that needs a different field group and a separate rule for each.

    Ok sorry I understand the issue now and I have recreated the issue on my local install.

    On the line of code below you are looking for the post_parent but I think you should be looking for the ID.

    I changed this:

    $post_parent = $post->post_parent;
    

    to this:

    $post_parent = $post->ID;
    

    and it's working for me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误