我有以下代码,我在保存帖子时从acf字段field_611eb3690a472更新car_year分类法字段,在第二个函数中,我试图从acf字段field_612cfc339a8ba更新car_fuel_type,但同时根据acf字段值更改值。第一个car_year字段更新工作得很好,但是car_fuel_type字段没有得到任何值。
为什么?你能张贴一个类似的例子,这样我就能看到问题是什么?
下面是我在functions.php文件中的代码:add_action('save_post', '__hp_frd_year');
function __hp_frd_year($post_id) {
$release_date = !empty($_POST['acf']['field_611eb3690a472']) ? $_POST['acf']['field_611eb3690a472'] : '';
$year = !empty($_POST['acf']['field_588f336aabaa6']) ? $_POST['acf']['field_588f336aabaa6'] : '';
$release_date_year = date("Y", strtotime($release_date) );
$new_term = wp_insert_term(
$release_date_year, // the term
'car_year', // the taxonomy
array(
'description' => '',
'slug' => strtolower($release_date_year),
)
);
if(!is_wp_error($new_term)) {
wp_set_object_terms( $post_id, $new_term['term_id'], 'car_year' );
} else {
$term_added = wp_set_object_terms( $post_id, $new_term->error_data['term_exists'], 'car_year' );
}
}
/**
* Fuel type data and fuel_type acf fields
*/
add_action('save_post', '__hp_fuel_type');
function __hp_fuel_type($post_id) {
$fuel_type_acf = !empty($_POST['acf']['field_612cfc339a8ba']) ? $_POST['acf']['field_612cfc339a8ba'] : '';
$fuel_type_acf_lang == '';
if ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΒΕΝΖΙΝΗ') {
// if ($fuel_type_acf == "ΒΕΝΖΙΝΗ"){
$fuel_type_acf_lang = '[:el]ΒΕΝΖΙΝΗ[:en]UNLEADED[:]';
}else{if ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΠΕΤΡΕΛΑΙΟ') {
// if ($fuel_type_acf == "ΠΕΤΡΕΛΑΙΟ"){
$fuel_type_acf_lang = '[:el]ΠΕΤΡΕΛΑΙΟ[:en]DIESEL[:]';
}else{if ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΑΕΡΙΟ') {
// if ($fuel_type_acf == "ΑΕΡΙΟ"){
$fuel_type_acf_lang = '[:el]ΑΕΡΙΟ[:en]GAS[:]';
}else{if ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΥΒΡΙΔΙΚΟ / ΒΕΝΖΙΝΗ') {
// if ($fuel_type_acf == "ΥΒΡΙΔΙΚΟ / ΒΕΝΖΙΝΗ"){
$fuel_type_acf_lang = '[:el]ΥΒΡΙΔΙΚΟ / ΒΕΝΖΙΝΗ[:en]HYBRID / UNLEADED[:]';
}else{if ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΥΒΡΙΔΙΚΟ / ΠΕΤΡΕΛΑΙΟ') {
// if ($fuel_type_acf == "ΥΒΡΙΔΙΚΟ / ΠΕΤΡΕΛΑΙΟ"){
$fuel_type_acf_lang = '[:el]ΥΒΡΙΔΙΚΟ / ΠΕΤΡΕΛΑΙΟ[:en]HYBRID / DIESEL[:]';
}else{if ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΗΛΕΚΤΡΙΚΟ') {
// if ($fuel_type_acf == "ΗΛΕΚΤΡΙΚΟ"){
$fuel_type_acf_lang = '[:el]ΗΛΕΚΤΡΙΚΟ[:en]ELECTRIC[:]';
}}}}}}
$new_term = wp_insert_term(
$fuel_type_acf_lang, // the term
'car_fuel_type', // the taxonomy
array(
'description' => '',
'slug' => strtolower($fuel_type_acf_lang),
)
);
if(!is_wp_error($new_term)) {
wp_set_object_terms( $post_id, $new_term['term_id'], 'car_fuel_type' );
} else {
$term_added = wp_set_object_terms( $post_id, $new_term->error_data['term_exists'], 'car_fuel_type' );
}
}
感谢我终于想通了:
add_action('save_post', '__hp_fuel_type');
function __hp_fuel_type($post_id) {
$fuel_type_acf = !empty($_POST['acf']['field_612cfc339a8ba']) ? $_POST['acf']['field_612cfc339a8ba'] : '';
if (((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΒΕΝΖΙΝΗ') || ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'UNLEADED')) {
$fuel_type_acf = '[:el]ΒΕΝΖΙΝΗ[:en]UNLEADED[:]';
}else{if (((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΠΕΤΡΕΛΑΙΟ') || ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'DIESEL')) {
$fuel_type_acf = '[:el]ΠΕΤΡΕΛΑΙΟ[:en]DIESEL[:]';
}else{if (((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΑΕΡΙΟ') || ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'GAS')) {
$fuel_type_ac = '[:el]ΑΕΡΙΟ[:en]GAS[:]';
}else{if (((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΥΒΡΙΔΙΚΟ / ΒΕΝΖΙΝΗ') || ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'HYBRID / UNLEADED')) {
$fuel_type_acf = '[:el]ΥΒΡΙΔΙΚΟ / ΒΕΝΖΙΝΗ[:en]HYBRID / UNLEADED[:]';
}else{if (((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΥΒΡΙΔΙΚΟ / ΠΕΤΡΕΛΑΙΟ') || ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'HYBRID / DIESEL')) {
$fuel_type_acf = '[:el]ΥΒΡΙΔΙΚΟ / ΠΕΤΡΕΛΑΙΟ[:en]HYBRID / DIESEL[:]';
}else{if (((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ΗΛΕΚΤΡΙΚΟ') || ((isset($fuel_type_acf) ? $fuel_type_acf : null) == 'ELECTRIC')) {
$fuel_type_acf = '[:el]ΗΛΕΚΤΡΙΚΟ[:en]ELECTRIC[:]';}}}}}}
$new_term = wp_insert_term(
$fuel_type_acf, // the term
'car_fuel_type', // the taxonomy
array(
'description' => '',
'slug' => strtolower($fuel_type_acf),
)
);
if(!is_wp_error($new_term)) {
wp_set_object_terms( $post_id, $new_term['term_id'], 'car_fuel_type' );
} else {
$term_added = wp_set_object_terms( $post_id, $new_term->error_data['term_exists'], 'car_fuel_type' );
}}