从自定义帖子类型字段创建页面标题

时间:2011-02-07 19:59:30

标签: wordpress-theming custom-post-type

我想根据用户输入的自定义字段创建自定义类型帖子的标题。例如,我有字段'名字'和'姓氏',并且希望根据用户输入的内容创建标题,并且永久链接也可以从中创建slug。

我在powers.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行,它就会挂起。

1 个答案:

答案 0 :(得分: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发布的表单变量中获取你的值。

干杯!