我一直在努力实现这一点,但没有成功。我有一个自定义的帖子类型“Contributor”,在这里我禁用了默认的标题字段,我正试图找出如何根据高级自定义字段中的四个值设置自定义标题:
“公司”吗复选框公司名称文本字段(如果选中了“公司”)给定名称和姓氏文本字段的组合(如果未选中“公司”)我正在使用此函数来组合管理列的全名,例如:
function biwp_contributor_name($post_id) {
$corporate = get_field(\'biwp_corporate\', $post_id ); //get Corporate checkbox value
if ($corporate) { //if Corporate is checked, use biwp_cname (Corportate Name)
$name = get_field(\'biwp_cname\', $post_id );
} else { //if Corporate is unchecked, use biwp_gnames (Given Names) and biwp_snames (Surnames)
$gnames = get_field(\'biwp_gnames\', $post_id );
$snames = get_field(\'biwp_snames\', $post_id );
$name = $gnames .\' \'. $snames;
}
return $name;
}
这对于展示来说非常有用。但问题是WP中的许多内容都依赖于默认标题,因此使用该函数来实际创建帖子标题(和slug)会更好。我已经看到了一些关于这个主题的其他帖子,尝试了一些不同的解决方案,但都没有用。这一条来自this ACF forum thread:
add_filter(\'acf/update_value\', \'biwp_fix_contributor_title\', 10, 3);
function biwp_fix_contributor_title( $value, $post_id, $field ) {
if (get_post_type($post_id) == \'contributor\') {
$name = biwp_contributor_name($post_id);
$new_title = $name;
$new_slug = sanitize_title( $new_title );
// update post
$biwp_contributor = array(
\'ID\' => $post_id,
\'post_title\' => $new_title,
\'post_name\' => $new_slug,
);
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\', \'biwp_fix_contributor_title\');
// update the post, which calls save_post again
wp_update_post( $biwp_contributor );
// re-hook this function
add_action(\'save_post\', \'biwp_fix_contributor_title\');
}
}
return $value;
}
这个是this SE thread:add_action(\'save_post\', \'biwp_fix_contributor_title\', 12);
function biwp_fix_contributor_title ($post_id) {
if ( $post_id == null || empty($_POST) )
return;
if ( !isset( $_POST[\'post_type\'] ) || $_POST[\'post_type\']!=\'contributor\' )
return;
if ( wp_is_post_revision( $post_id ) )
$post_id = wp_is_post_revision( $post_id );
global $post;
if ( empty( $post ) )
$post = get_post($post_id);
if ($_POST[\'biwp_corporate\']!=\'\') {
global $wpdb;
$name = biwp_contributor_name($post_id);
$where = array( \'ID\' => $post_id );
$wpdb->update( $wpdb->posts, array( \'post_title\' => $name ), $where );
}
}
但这两者似乎都没有起到任何实际作用,甚至都没有产生负面的、令人不快的影响,这真的让我感到奇怪。提前感谢您的帮助。