2011-02-07 77 views
0

我想根據用戶輸入的自定義字段創建自定義類型帖子的標題。例如,我有字段'名字'和'姓氏',並希望從用戶輸入的任何內容和永久鏈接創建標題。從自定義帖子類型字段創建頁面標題

我已經試過這在我身後的functions.php聲明自定義類型的末尾:

function save_member(){ 
global $post; 
update_post_meta($post->ID, "fname", $_POST["fname"]); 
update_post_meta($post->ID, "lname", $_POST["lname"]); 
$this_post = array(); 
$this_post['ID'] = $post->ID; 
$this_post['post_title'] = $_POST["fname"].' '.$_POST["lname"]; 
wp_update_post($this_post); 
} 

,如果我添加代碼的最後4行以上它只是掛起。

回答

0

有可能是您在發起某個do_action()調用(或相關的函數調用)時調用wp_update_post時啓動循環。

這樣做:

public static function repair_title($title){ 
    global $post; 

    // only do this for a specific type, may want to 
    // expand this as needed 
    if("your_type" != $post->post_type) return $title; 

    $firstname  = $_POST["firstname"]; 
    $lastname   = $_POST["firstname"]; 

    $title = $firstname . " " . $lastname; 

    return $title; 
} 

public static function repair_name($name){ 
    global $post; 
    if("your_type" != $post->post_type) return $name; 

    $firstname  = $_POST["firstname"]; 
    $lastname   = $_POST["firstname"]; 

    $name = wp_unique_post_slug(
    $firstname . " " . $lastname, 
    $post->ID, 
    $post->post_type, 
    0 
); 

    return $name; 
} 

編輯:改變了這個在提一點我自己的代碼中的錯誤的(我只是碰巧在這方面的工作目前)。我正在使用get_post_custom(),並且在post對象更改之前觸發它,所以您需要從$ _POST發佈的表單變量中獲取值。

乾杯!

相關問題