Registering a custom post type comes in handy pretty often when I develop WordPress solutions for my clients. Usually it’s for some kind of content, where there needs to be an archive or similar. I’ve used it for making custom galleries or for an employee to easily register and update football players in their squad.
This little walkthrough is just as much for my own sake as for yours. I have this little quirk where I somehow always end up selecting the wrong link to the WP codex, where the following snippet is copied from. So might as well make my own little page about it, where I can copy it from for future purposes.
Take the following snippet and insert it into your
functions.php. I would always recommend you to make use of a child theme, so that you can recieve updates to your theme.
What do I need to change?
You would want to change the following in order to make the custom post type visible:
This should be the text domain of your current theme. You can always find this by opening the
style.css of your theme. It will in the top say
( 'slug' => 'book' )
Here you would want to change the
book part. This is what is going to be in your url. Fx.
This is the function you are creating, to make the custom post type available. I would probably call it something like
players_init() if it had something to do with football players. Remember to change it both at the
functions part and
add_action in the beginning.
This is all the different places the name will appear in your WordPress dashboard. You can kind of see the purpose of the different labels. F.e.
menu_name is what it will be displayed as in your dashboard menu on the left.
register_post_type( 'book', $args );
This needs to be the same as your
( 'slug' => 'book' )-part. It is the part telling your WordPress installation, that you are registering a new post type.
Remember to save your permalinks
Once you’ve added the snippet to your
functions.php and see it’s working on your website, it is very important to go to Settings and then Permalinks. Here you just need to scroll to the bottom and press Save changes, to make sure that your new post type is working correctly. The new posts might not show and pop up with a error 404: page not found, if you don’t go in and re-save your permalinks