我想自動生成這樣的帖子標題 例如:ABC-123456 我還需要讓((ABC-))固定并隨機更改 06 數字,并且不要通過更新帖子來更改帖子標題
uj5u.com熱心網友回復:
首先,要以正確的方式修改 WordPress 行為,您需要找到一個合適的鉤子。在這種情況下,這將是一個過濾器,允許在將 Post 資料保存到資料庫之前對其進行更改。
過濾器'wp_insert_post_data'正是您所需要的,因此您添加過濾器,并將其連接到如下函式:
function zozson_filter_post_title(){
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
' wp_insert_post_data ' 是過濾器的名稱
' zozson_filter_post_title ' 是你給你的函式起的名字,用來掛鉤它。
50是優先級。在大多數其他事情之后,我選擇了 50 來運行它。默認值為 10
4是過濾器傳遞給您的函式的變數數。
所以現在我們將添加這些變數和其中的邏輯,將這些 CPT sho7nat 分配給管理員保存它們的那些標題。
function zozson_filter_post_title( $data, $postarr, $unsanitized_postarr, $update){
//Then if it is the post type sho7nat
if( $data['post_type'] !== 'sho7nat' ){
return $data;
}
//If there is already a titled saved ($update returns true always)
if( $data['post_title'] !== '' ){
return $data;
}
//Let's build our title
$post_title = ' ABC-';
//What better random number that a unique timestamp?
$random_number = strtotime('now');
//Add the random number to the post title to save. You can do these in 1 line instead of 3
$post_title.= $random_number;
//We now have a post title with ABC- fixed and a random number, tell WordPress to use it as the post title
$data['post_title'] = $post_title;
return $data;
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
自動分配的標題應如本例所示:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/523062.html
上一篇:將受密碼保護的檔案上傳到服務器
