恐怕上面的代码根本不起作用。我这样修改它:
add_filter( \'wp_insert_post_data\', \'wpse_update_post_title_and_slug\' );
function wpse_update_post_title_and_slug( $data , $postarr ) {
//You can make some checks here before modify the post data.
//For example, limit the changes to standard post type
if( \'people\' == $data[\'post_type\'] ) {
//Add post meta to post title
$data[\'post_title\'] = $_POST[\'first_name\'] . \' \' . $_POST[\'last_name\'];
//Update the slug of the post for the URL
$data[\'post_name\'] = sanitize_title( $data[\'post_title\'] );
}
return $data;
}
尽管存在特定的自定义字段并具有内容,但标题和slug都不会更改。
我想在保住职位后,两者都会被“强行”修改?或者可能不是那样的?
我还尝试这样重新编码:
function wpse_update_post_title_and_slug( $data , $postarr ) {
if( $data[\'post_type\'] == \'people\' ) {
// get post data
$id = $postarr[\'ID\'];
$first_name = get_post_meta( $id, \'first_name\', true );
$last_name = get_post_meta( $id, \'last_name\', true );
// insert the data into the title and the slug
$data[\'post_title\'] = $first_name . \' \' . $last_name;
$data[\'post_name\'] = sanitize_title( $data[\'post_title\'] );
}
return $data;
}
add_filter( \'wp_insert_post_data\', \'wpse_update_post_title_and_slug\' );
但都是一样的。没有反应,没有错误,什么都没有。因为它完全没有功能。php。它正常工作。