如何在函数内部检索(ACF)中继器第一行和最后一行的子字段值?

时间:2020-07-15 作者:L. core

我正在使用一个函数,根据您在某些自定义字段中输入的内容,更改自定义帖子类型的前端和后端帖子标题。

    //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子字段的值。

注意:中继器具有随机数目的行。

2 个回复
最合适的回答,由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);
    
    }

}

SO网友:L. core

我在代码中所需要的是:

    //get the repeater
    $car_repeater = get_field(\'repeater_name\',$post_id); 
    
    
    $first_row = $car_repeater[0]; //put [0] to get the first row of the repeater
    $first_car_model = $first_row[\'car_model\']; //put [car_model] to get this field value in the specified row (first row)
    
    
    $last_row = end($car_repeater); //use end() to get the last row of the repeater
    $last_car_model = $last_row[\'car_model\']; //put [car_model] to get this field value in the specified row (last row)

相关推荐

Prevent loading of functions

我使用了一个主题,其中包括名为“Sharethis”的服务的共享功能和字体真棒。据我所知,函数是从函数中的以下行加载的。php。TT\\u队列::add\\u css(数组(“//netdna.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css”);TT\\u排队::add\\u js(array(\'https://ws.sharethis.com/button/buttons.js\'));如何使用子函数(因为父函数中的更改将在更新