从自定义帖子类型字段创建页面标题
问题描述:
我想根据用户输入的自定义字段创建自定义类型帖子的标题。例如,我有字段'名字'和'姓氏',并希望从用户输入的任何内容和永久链接创建标题。从自定义帖子类型字段创建页面标题
我已经试过这在我身后的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行以上它只是挂起。
答
有可能是您在发起某个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发布的表单变量中获取值。
干杯!