Wordpress在创建新的自定义帖子时设置默认标题

时间:2013-05-30 08:55:16

标签: wordpress-plugin wordpress

我的自定义帖子类型只包含两个(文本)字段:创建自定义帖子类型时使用'supports' => array('title')的ISBN编号和youtube网址。

问题是,我不需要标题。因此,当我保存我的帖子时,我就这样做,以便标题成为ISBN号。

  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;
  }

这样可行,但问题是,当我保存帖子而不预先填写标题(任何内容)时,帖子未保存,标题更改功能未被调用,我的所有字段都被重置。

是否可以为标题设置默认值并隐藏它?

1 个答案:

答案 0 :(得分:1)

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

当您致电register_post_type()时,请向名为$args的{​​{1}}添加另一个条目,并将其值设置为数组。然后,您可以传递要支持的帖子类型的元素列表。默认为“标题”和“编辑器”,但有许多选项可供选择。

例如:

supports

只要错过<?php register_post_type( "myCustomPostType", array( 'supports' : array( 'editor', 'author', 'custom-fields' ) ) ) ?> ,您就不必为每个帖子定义一个。

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

相关问题