Wordpress 在创建新的自定义帖子时设置默认标题 [英] Wordpress set default title when creating a new custom post

查看:30
本文介绍了Wordpress 在创建新的自定义帖子时设置默认标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有两个(文本)字段的自定义帖子类型:一个 ISBN 号和一个 youtube url,使用 'supports' =>array('title') 创建我的自定义帖子类型时.

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.

问题是,我不需要标题.所以当我保存我的帖子时,我把标题变成了 ISBN 号.

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 ?

推荐答案

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

When you register your custom post type, you can set what it supports, including the title.

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

When you call register_post_type(), add another entry to $args called supports and set it's value to an array. You can then pass a list of elements you want that post type to support. The default is 'title' and 'editor', but there are a host of options to choose from.

例如:

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

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

So long as you miss out title then you won't have to define one for each post.

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

For more information, visit this page: http://codex.wordpress.org/Function_Reference/register_post_type#Arguments

这篇关于Wordpress 在创建新的自定义帖子时设置默认标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆