Vin*_*kar 5 php wordpress advanced-custom-fields
我有一个名为"Designer"的自定义帖子类型每个帖子将使用不同的唯一高级自定义字段,因为每个帖子都有唯一的模板.通过以下代码,我可以为Designer帖子类型中的每个帖子提供规则并保存但自定义字段没有显示在后端的帖子编辑页面上.通常这个代码应该ork但不知道代码发生了什么
请帮忙.
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;
}
Run Code Online (Sandbox Code Playgroud)
您的Artist Collection字段组设置为仅显示在一个帖子上,即帖子设计师Post 1,它是Designer Post类型.
我不明白所有的代码是什么?只需为每个需要不同字段组的帖子创建不同的字段组,并为每个字段创建单独的规则.
好的抱歉,我现在理解了这个问题,并在本地安装中重新创建了该问题.
在下面的代码行中,您正在寻找post_parent,但我认为您应该寻找ID.
我改变了这个:
$post_parent = $post->post_parent;
Run Code Online (Sandbox Code Playgroud)
对此:
$post_parent = $post->ID;
Run Code Online (Sandbox Code Playgroud)
它对我有用.
| 归档时间: |
|
| 查看次数: |
1438 次 |
| 最近记录: |