September 6, 2024
functions.php
FileFirst, you need to access your theme’s functions.php
file. You can find this file in your WordPress dashboard under Appearance > Theme Editor.
Alternatively, you can access it via FTP or through your hosting provider’s file manager. Look for wp-content/themes/your-theme/functions.php
.
You’re going to add some code to functions.php
. Here’s a basic example for creating a custom post type called “Book.”
// Register Custom Post Type
function create_book_post_type() {
$labels = array(
'name' => _x( 'Books', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Book', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Books', 'text_domain' ),
'name_admin_bar' => __( 'Book', 'text_domain' ),
'archives' => __( 'Book Archives', 'text_domain' ),
'attributes' => __( 'Book Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Book:', 'text_domain' ),
'all_items' => __( 'All Books', 'text_domain' ),
'add_new_item' => __( 'Add New Book', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Book', 'text_domain' ),
'edit_item' => __( 'Edit Book', 'text_domain' ),
'update_item' => __( 'Update Book', 'text_domain' ),
'view_item' => __( 'View Book', 'text_domain' ),
'view_items' => __( 'View Books', 'text_domain' ),
'search_items' => __( 'Search Book', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into book', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this book', 'text_domain' ),
'items_list' => __( 'Books list', 'text_domain' ),
'items_list_navigation' => __( 'Books list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter books list', 'text_domain' ),
);
$args = array(
'label' => __( 'Book', 'text_domain' ),
'description' => __( 'Post Type Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'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',
);
register_post_type( 'book', $args );
}
add_action( 'init', 'create_book_post_type', 0 );
function create_book_post_type()
: This is a custom function we’re creating to register the new post type.$labels
: This array contains all the labels for the custom post type. This is how the custom post type will appear in the WordPress dashboard and other places.$args
: This array contains settings for the post type. Things like what features it supports (e.g., title, editor), whether it should be publicly accessible, and if it should show up in the admin menu.register_post_type( 'book', $args )
: This function registers the post type with WordPress using the labels and arguments we defined.add_action( 'init', 'create_book_post_type', 0 );
: This tells WordPress to run our create_book_post_type
function when it initializes.After you’ve added the code, save your functions.php
file. Go back to your WordPress dashboard, and you should see a new menu item called “Books.” Click on it, and you’ll see options to add and manage your custom posts.