How to Add Custom Post Types in WordPress Easily
Explanation
Creating a custom post type in WordPress allows you to add new content types beyond the default posts and pages. In this example, we're adding a custom post type called Book.
Here's what the code does:
- Labels: These are the names and descriptions you'll see in the WordPress admin area. For example, "Add New Book" or "Edit Book".
- Public: Setting this to true makes the books visible on the front end of your site.
- Publicly Queryable: Allows the books to be queried and displayed on your site.
- Show UI: Enables the user interface in the admin area for managing books.
- Show in Menu: Adds the books to the admin menu for easy access.
- Rewrite: This changes the URL structure to include 'book', making it more readable (e.g., yoursite.com/book/).
- Has Archive: Allows you to have an archive page for all books, similar to how blog posts have an archive.
- Supports: Specifies what features the books will support, like title, editor, and thumbnail.
Once this code is added to your theme's functions.php file, you'll see a new Books section in your WordPress admin menu. You can add, edit, and manage books just like regular posts.
Code
<?php
// Hook into the 'init' action
add_action('init', 'wp_dudecom_register_custom_post_type');
/**
* Register a custom post type called 'book'.
*
* @return void
*/
function wp_dudecom_register_custom_post_type() {
$labels = array(
'name' => _x('Books', 'Post type general name', 'textdomain'),
'singular_name' => _x('Book', 'Post type singular name', 'textdomain'),
'menu_name' => _x('Books', 'Admin Menu text', 'textdomain'),
'name_admin_bar' => _x('Book', 'Add New on Toolbar', 'textdomain'),
'add_new' => __('Add New', 'textdomain'),
'add_new_item' => __('Add New Book', 'textdomain'),
'new_item' => __('New Book', 'textdomain'),
'edit_item' => __('Edit Book', 'textdomain'),
'view_item' => __('View Book', 'textdomain'),
'all_items' => __('All Books', 'textdomain'),
'search_items' => __('Search Books', 'textdomain'),
'parent_item_colon' => __('Parent Books:', 'textdomain'),
'not_found' => __('No books found.', 'textdomain'),
'not_found_in_trash' => __('No books found in Trash.', 'textdomain'),
'featured_image' => _x('Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain'),
'set_featured_image' => _x('Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain'),
'remove_featured_image' => _x('Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain'),
'use_featured_image' => _x('Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain'),
'archives' => _x('Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain'),
'insert_into_item' => _x('Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain'),
'uploaded_to_this_item' => _x('Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain'),
'filter_items_list' => _x('Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain'),
'items_list_navigation' => _x('Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain'),
'items_list' => _x('Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'book'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
);
register_post_type('book', $args);
}
?>
Instructions
File Location: Add the code to your theme's functions.php
file or a custom plugin file if you prefer to keep it separate from your theme.
Prerequisites: No additional plugins or settings are required for this implementation.
Implementation Steps:
- Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
- Locate the
functions.php
File: Navigate towp-content/themes/your-active-theme/
and open thefunctions.php
file for editing. - Insert the Code: Copy the provided code snippet and paste it at the end of the
functions.php
file. Ensure you do not paste it within any existing function or PHP tag. - Save Changes: Save the
functions.php
file and close the editor. - Verify in WordPress Admin: Log in to your WordPress admin dashboard. You should now see a new menu item labeled Books in the admin menu.
- Add New Books: Click on Books in the admin menu to start adding, editing, and managing your custom post type entries.
If you encounter any issues or need further assistance with custom post types or other advanced WordPress functionalities, consider reaching out to wp-dude.com for expert help.