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

问题描述

我有一个 Wordpress 自定义帖子类型精选书",它使用副标题来指定书名.例如:精选书籍:饥饿游戏"

I have a Wordpress custom post type "Featured Book", which uses a subtitle to specify title of the book. Eg: "Featured Book: The Hunger Games"

是否可以将帖子标题自动设置为精选书"?

Is it possible to automatically set the title of the post as "Featured Book" ?

我通过谷歌搜索找到了这段代码,但它不起作用(Wordpress后端返回空白页/白屏死机)

I found this code through Googling but it does not work (Wordpress backend returns a blank page/white screen of death)

代码中是否存在语法错误,或者这根本不可能?

Is there some syntax error in the code, or is this just not possible?

function add_custom_title( $data, $postarr ) {
  if($data['post_type'] == 'featured-book') {
    if(empty($data['post_title'])) {
      $data['post_title'] = 'Featured Book';
    }
  }
  return $data;
}
add_filters('wp_insert_post_data', 'add_custom_title', 10, 2 );

推荐答案

你有一个错字:

add_filter('wp_insert_post_data', 'add_custom_title', 10, 2 );

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

08-14 09:36