$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom post registration causing errors|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom post registration causing errors

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far