首先,我很抱歉我的英语很差,当我升级一篇帖子时,这段代码将帖子slug更改为“profileid”costom字段值。。
add_action(\'save_post\', \'my_custom_slug\');
function my_custom_slug($post_id) {
//Check it\'s not an auto save routine
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
//Perform permission checks! For example:
if ( !current_user_can(\'edit_post\', $post_id) )
return;
//If calling wp_update_post, unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\', \'my_custom_slug\');
//call wp_update_post update, which calls save_post again. E.g:
wp_update_post(array(\'ID\' => $post_id, \'post_name\' =>get_post_meta($post_id,\'profileid\',true)));
// re-hook this function
add_action(\'save_post\', \'my_custom_slug\');
}
它工作得很好,但我如何才能将其仅用于特定的自定义帖子类型?我的自定义帖子类型是“masters”。。我用过这个,但不管用!有人能帮忙吗?
add_action(\'save_post\', \'my_custom_slug\');
function my_custom_slug($post_id) {
$slug = \'masters\';
// If this isn\'t a \'masters\' post, don\'t update it.
if ( $slug != $post->post_type )
return $post_id;
//Check it\'s not an auto save routine
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
//Perform permission checks! For example:
if ( !current_user_can(\'edit_post\', $post_id) )
return;
//If calling wp_update_post, unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\', \'my_custom_slug\');
//call wp_update_post update, which calls save_post again. E.g:
wp_update_post(array(\'ID\' => $post_id, \'post_name\' =>get_post_meta($post_id,\'profileid\',true)));
// re-hook this function
add_action(\'save_post\', \'my_custom_slug\');
}