Custom post registration causing errors

admin2025-06-03  3

I copied this code from a youtuber. It works for him, but not for me. Does the code contain any errors? As soon as I put it in, my WordPress breaks and won’t load.

function gt_custom_post_type() {
  register_post_type('project',
    array(
      'rewrite' => array('slug' => 'projects'),
      'labels' => array(
        'name' => 'Projects'
        'singular_name' => 'Project',
        'add_new_item' => 'Add New Project',
        'edit_item' => 'Edit Project'
      ),
      'menu-icon' => 'dashicons-media-document',
      'public' => true,
      'has_archive' => true,
      'supports' => array(
        'title', 'thumnail', 'editor', 'excerpt', 'comments'
      )
    )
  );
}

add_action('init', 'gt_custom_post_type');

I copied this code from a youtuber. It works for him, but not for me. Does the code contain any errors? As soon as I put it in, my WordPress breaks and won’t load.

function gt_custom_post_type() {
  register_post_type('project',
    array(
      'rewrite' => array('slug' => 'projects'),
      'labels' => array(
        'name' => 'Projects'
        'singular_name' => 'Project',
        'add_new_item' => 'Add New Project',
        'edit_item' => 'Edit Project'
      ),
      'menu-icon' => 'dashicons-media-document',
      'public' => true,
      'has_archive' => true,
      'supports' => array(
        'title', 'thumnail', 'editor', 'excerpt', 'comments'
      )
    )
  );
}

add_action('init', 'gt_custom_post_type');
Share Improve this question edited Feb 10, 2019 at 15:00 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 10, 2019 at 14:50 Ala'a RifiAla'a Rifi 132 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

There are many small problems with this code (see my comments below):

function gt_custom_post_type() {
  register_post_type('project',
    array(
      'rewrite' => array('slug' => 'projects'),
      'labels' => array(
        'name' => 'Projects', // <- missing comma
        'singular_name' => 'Project', 
        'add_new_item' => 'Add New Project',
        'edit_item' => 'Edit Project'
      ),
      'menu-icon' => 'dashicons-media-document',
      'public' => true,
      'has_archive' => true,
      'supports' => array(
        'title', 'thumbnail', 'editor', 'excerpt', 'comments' // <- thumbnail not thumnail
      )
    )
  );
}
add_action('init', 'gt_custom_post_type');

Also you’re lacking any internationalization in there...

I'd recommend using this resource for getting custom post type code to add to your functions.php: https://generatewp/post-type/ If you're finding that you're still running into errors after using that code, then the issue is likely how you're adding it to your functions.php file.

There's also a great plugin called Custom Post Types.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748922569a314846.html

最新回复(0)