0

I have a custom post type with just two (text)fields: an ISBN number and a youtube url by using 'supports' => array('title') when creating my custom post type.

The problem is, I don't need a title. So when I save my post, I made it so that the title becomes the ISBN number.

  add_filter('wp_insert_post_data', array($this, 'change_title'), 99, 2);

  function change_title($data, $postarr) {
    if ($data['post_type'] == 'book_video') {
      // If it is our form has not been submitted, so we dont want to do anything
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $data;

      // Verify this came from the our screen and with proper authorization because save_post can be triggered at other times
      if (!isset($_POST['wp_meta_box_nonce']))
        return $data;

      // Combine address with term
      $title = $_POST['_bv_isbn'];
      $data['post_title'] = $title;
    }
    return $data;
  }

This works, but the problem is, when I save the post WITHOUT prefilling a title (anything at all) the post is not saved, the title change function is not called, and all of my fields are reset.

Is it possible to set a default value to the title and hide it ?

4

1 回答 1

1

当您注册自定义帖子类型时,您可以设置它支持的内容,包括标题。

当您调用时register_post_type(),将另一个条目添加到$args被调用supports并将其值设置为数组。然后,您可以传递您希望该帖子类型支持的元素列表。默认值为“标题”和“编辑器”,但有许多选项可供选择。

例如:

<?php 
  register_post_type( 
    "myCustomPostType", 
    array(
      'supports' : array(
        'editor',
        'author',
        'custom-fields'
      )
    )
  )
?>

只要您错过了title,您就不必为每个帖子定义一个。

有关更多信息,请访问此页面:http ://codex.wordpress.org/Function_Reference/register_post_type#Arguments

于 2013-05-30T09:06:58.993 回答