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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化