自定义帖子类型和自定义字段在Wordpress多站点


Custom Post types and Custom fields in Wordpress Multisite

我是Wordpress Multisite的新手,我想知道是否有可能为每个新网站定义一组自定义帖子类型(电影,类别和演员)和一组自定义字段(即电影有一个预告片字段,演员和类别)。

我需要能够创建新的网站,所有这些网站都必须预先配置CPT和CF我说。

是否有可能与Wordpress MU或我应该找到另一个解决方案?

如果它们都使用相同的主题,您可以在父主题的functions.php中声明每个自定义帖子类型(如果您有子主题)。由于多个站点共享相同的插件和主题,自定义帖子类型将为使用该主题的所有站点注册。

自定义字段略有不同。你可以使用像高级自定义字段这样的插件,这是我推荐的,但这需要为每个站点激活。如果您希望这些自定义字段可以立即在帖子编辑屏幕上使用,您可以将每个必要字段的add_post_meta()添加到ID为1的帖子中。Wordpress的所有实例都会有一个ID为1的帖子,这是默认的帖子。然后这些字段将被"预注册"。请注意,我省略了你提到的"Categories"帖子类型和字段,因为Category已经是Wordpress的一个分类,它可能会让用户感到困惑。这不是一个理想的解决方案,但它有效。我在为一个自动部署Wordpress实例的牙科办公室软件预填充自定义字段时不得不这样做。

add_action( 'init', 'create_custom_post_types' );
function create_custom_post_types() {
  register_post_type( 'films',
    array(
      'labels' => array(
        'name' => __( 'Films' ),
        'singular_name' => __( 'Film' )
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'custom-fields' )
    )
  );
  register_post_type( 'actors',
    array(
      'labels' => array(
        'name' => __( 'Actors' ),
        'singular_name' => __( 'Actor' )
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'custom-fields' )
    )
  );
}
// Check if the post meta has been added to our default post. 
// If not, add our post_meta to make it available.
if(!get_post_meta( 1, 'trailer_for_film') ) {
  add_post_meta( 1, 'trailer_for_film', 'default trailer', true );
  add_post_meta( 1, 'actors', 'default actor', true );
}