我正在使用一个函数,根据您在某些自定义字段中输入的内容,更改自定义帖子类型的前端和后端帖子标题。
//Save ACF fields as post_content for back-end
add_action(\'save_post\', \'change_title_cars\');
function change_title_cars($post_id) {
global $_POST;
if(\'cars\'== get_post_type()){
$car_name = get_post_meta($post_id,\'car_name\',true);
$first_car_model = ????????????;
$last_car_model = ????????????;
$my_post = array();
$my_post[\'ID\'] = $post_id;
$my_post[\'post_title\'] = $car_name . \' - \' . $first_car_model . \' ~ \' . $last_car_model;
remove_action(\'save_post\', \'change_title_cars\');
wp_update_post( $my_post );
add_action(\'save_post\', \'change_title_cars\');
}
}
//Save ACF fields as post_content for front-end
add_action(\'acf/save_post\', \'change_title_frontend_cars\');
function change_title_frontend_cars($post_id) {
global $_POST;
if(\'cars\'== get_post_type()){
$car_name = get_post_meta($post_id,\'car_name\',true);
$first_car_model = ????????????;
$last_car_model = ????????????;
$my_post = array();
$my_post[\'ID\'] = $post_id;
$my_post[\'post_title\'] = $car_name . \' - \' . $first_car_model . \' ~ \' . $last_car_model;
remove_action(\'acf/save_post\', \'change_title_frontend_cars\');
wp_update_post( $my_post );
add_action(\'acf/save_post\', \'change_title_frontend_cars\');
}
}
我使用的中继器有一个子字段,名为;car\\U型号;。我想把标题改成这样:
(car\\u name)-(中继器中的第一个“car\\u model”~(中继器中的最后一个“car\\u model”)
但我无法获取函数中的repeater子字段的值。
注意:中继器具有随机数目的行。
最合适的回答,由SO网友:joshmoto 整理而成
我认为你不需要使用save_post
将后端另存为acf/save_post
在后端更新帖子时也会保存。我认为(95%肯定)。
阅读下面代码中的注释,看看这是否有助于您创建所需的自定义帖子标题。考虑到只有一种车型、多种车型和无车型的汽车。无论如何,这是一个开始。
// run modifications when creating/updating the car post type
add_action(\'acf/save_post\', \'acf_save_car\', 20);
/**
* action to run modifications when creating/updating the car post type
* @param int $post_id
* @return void
*/
function acf_save_car($post_id) {
// get our current global post object data
global $post;
// check we are on post type cars else return early
if($post->post_type <> \'cars\') return;
// if car post status is publish or draft
if($post->post_status == \'publish\' || $post->post_status == \'draft\') {
// create car object from current post object
$car = $post;
// get your car name custom field (acf way) and add to title array
$car_name = get_field(\'car_name\', $post_id);
// get your model acf repeater field (not sure of field name for repeater)
$model_data = get_field(\'car_repeater\', $post_id);
// set model span var as false incase no models exist
$model_span = false
// if model items exist in repeater
if($model_data) {
// create empty models array
$models = [];
// if only one model exists in repeater
if(count($model_data) === 1) {
// get the car model name and add it to our array
$models[] = $model_data[0][\'car_model\']
} else {
// get the first and last model data
$model_first = current($model_data);
$model_last = end($model_data);
// add this data to our model array
$models[] = $model_first[\'car_model\'];
$models[] = $model_last[\'car_model\'];
}
// create our model title segment with squiggly separator
$model_span = implode(\' ~ \', $models);
}
// empty car post title array
$car_post_title = []
// add car title segments to car post title array if they exist
$car_name ? $car_post_title[] = $car_name : false;
$model_span ? $car_post_title[] = $model_span : false;
// temp remove the save car action
remove_action(\'acf/save_post\', \'acf_save_car\', 20);
// update our car object with new data
$car->post_title = implode(\' - \', $car_post_title);
$car->post_name = sanitize_title($car->post_title);
// update the current post with updated car object
wp_update_post($car);
// re add the save car action
add_action(\'acf/save_post\', \'acf_save_car\', 20);
}
}