gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:59:47 25 4
gpt4 key购买 nike

我有一个名为“Designer”的自定义帖子类型,每个帖子都将使用不同的唯一高级自定义字段,因为每个帖子都有唯一的模板。使用下面的代码,我可以为 Designer 帖子类型中的每个帖子提供规则并保存,但是自定义字段未显示在后端的帖子编辑页面上。通常这段代码应该可以,但不知道代码发生了什么

请帮助。

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

最佳答案

您的 Artist Collection 字段组设置为仅出现在一个帖子上,帖子 Designer Post 1 这是一种 Designer Post 类型。

我不明白所有的代码是干什么用的?只需为每个需要不同字段组的帖子创建一个不同的字段组,并为每个帖子创建一个单独的规则。

好的,抱歉,我现在明白了这个问题,我已经在我的本地安装中重现了这个问题。

在下面的代码行中,您正在寻找 post_parent,但我认为您应该寻找 ID。

我改变了这个:

$post_parent = $post->post_parent;

为此:

$post_parent = $post->ID;

它对我有用。

关于php - 自定义字段未显示在自定义帖子类型帖子中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43066177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com