Custom Post Types WordPress

User
create custom post types wordpress
Assistant
Creating custom post types in WordPress is straightforward and can be done by adding a function to your theme's `functions.php` file or creating a custom plugin. Here's how you can create a custom post type named **"Books"**:

### Code Example to Add a Custom Post Type:

// Register Custom Post Type
function create_custom_post_type_books() {
    $labels = array(
        'name'                  => _x('Books', 'Post Type General Name', 'textdomain'),
        'singular_name'         => _x('Book', 'Post Type Singular Name', 'textdomain'),
        'menu_name'             => __('Books', 'textdomain'),
        'name_admin_bar'        => __('Book', 'textdomain'),
        'archives'              => __('Book Archives', 'textdomain'),
        'attributes'            => __('Book Attributes', 'textdomain'),
        'parent_item_colon'     => __('Parent Book:', 'textdomain'),
        'all_items'             => __('All Books', 'textdomain'),
        'add_new_item'          => __('Add New Book', 'textdomain'),
        'add_new'               => __('Add New', 'textdomain'),
        'new_item'              => __('New Book', 'textdomain'),
        'edit_item'             => __('Edit Book', 'textdomain'),
        'update_item'           => __('Update Book', 'textdomain'),
        'view_item'             => __('View Book', 'textdomain'),
        'view_items'            => __('View Books', 'textdomain'),
        'search_items'          => __('Search Book', 'textdomain'),
        'not_found'             => __('Not found', 'textdomain'),
        'not_found_in_trash'    => __('Not found in Trash', 'textdomain'),
        'featured_image'        => __('Featured Image', 'textdomain'),
        'set_featured_image'    => __('Set featured image', 'textdomain'),
        'remove_featured_image' => __('Remove featured image', 'textdomain'),
        'use_featured_image'    => __('Use as featured image', 'textdomain'),
        'insert_into_item'      => __('Insert into book', 'textdomain'),
        'uploaded_to_this_item' => __('Uploaded to this book', 'textdomain'),
        'items_list'            => __('Books list', 'textdomain'),
        'items_list_navigation' => __('Books list navigation', 'textdomain'),
        'filter_items_list'     => __('Filter books list', 'textdomain'),
    );
    $args = array(
        'label'                 => __('Book', 'textdomain'),
        'description'           => __('Post type for books', 'textdomain'),
        'labels'                => $labels,
        'supports'              => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
        'taxonomies'            => array('category', 'post_tag'),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-book',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
        'show_in_rest'          => true, // Enables Gutenberg editor
    );
    register_post_type('book', $args);
}
add_action('init', 'create_custom_post_type_books', 0);

### Key Points in the Code:
– **Labels**: Define how the custom post type will appear in the admin panel.
– **Supports**: Specify which WordPress features the post type supports (e.g., title, editor, thumbnail).
– **Menu Icon**: Set the icon using a [Dashicons](https://developer.wordpress.org/resource/dashicons/).
– **Show in REST**: Enables the post type to use the Gutenberg editor and APIs.
– **Has Archive**: Set to `true` to create an archive page for the custom post type.

### Activating the Custom Post Type:
– Add the code to your theme’s `functions.php` file or create a custom plugin.
– Visit the WordPress admin and refresh permalinks by navigating to **Settings > Permalinks** and clicking **Save Changes**.

### Customizing the Output:
You can customize the display of your custom post type using template files:
– **Single Post**: `single-book.php`
– **Archive Page**: `archive-book.php`

Create these files in your theme directory to control how individual posts and archives of the custom post type are displayed.

Leave a Comment

Your email address will not be published. Required fields are marked *