How to Create Custom Post Type in WordPress Without a Plugin

Create Custom Post Type in WordPress Without a Plugin

Last Updated: March 15, 2024By Tags: , ,

WordPress offers lots of customization options, and one powerful feature is the ability to create custom post types. It allows you to organize and display different types of content outside of standard posts and pages. Although there are plugins that simplify this process, you can create custom post types without relying on additional plugins. In this guide, we’ll walk you through the steps to create custom post type called “Portfolio” using native WordPress functionality.

Why Create Custom Post Type?

WordPress traditionally comes with two main content types: posts and pages. However, there are situations where you may want to introduce a new content type to suit your site’s unique requirements. This is where custom post types come into play. They allow you to configure and display different types of content outside of the default options.

 

Step 1: Access the Theme Files

To get started, access your WordPress theme files. You can do this through the WordPress dashboard by navigating to “Appearance” and then “Theme Editor.” Alternatively, use an FTP client to access the theme files directly.

Step 2: Open the functions.php File

In the Theme Editor, locate and open the functions.php file. This file contains essential functions for your theme, and it’s where we’ll be adding our custom post type code.

Step 3: Add Code for Create Custom Post Type

Insert the following code at the end of your functions.php file for the Register custom post type:

// Register Custom Post Type
function custom_portfolio_post_type() {
    $labels = array(
        'name'               => _x( 'Portfolio', 'Post Type General Name', 'text_domain' ),
        'singular_name'      => _x( 'Portfolio Item', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'          => __( 'Portfolio', 'text_domain' ),
        'parent_item_colon'  => __( 'Parent Portfolio Item:', 'text_domain' ),
        'all_items'          => __( 'All Portfolio Items', 'text_domain' ),
        'view_item'          => __( 'View Portfolio Item', 'text_domain' ),
        'add_new_item'       => __( 'Add New Portfolio Item', 'text_domain' ),
        'add_new'            => __( 'Add New', 'text_domain' ),
        'edit_item'          => __( 'Edit Portfolio Item', 'text_domain' ),
        'update_item'        => __( 'Update Portfolio Item', 'text_domain' ),
        'search_items'       => __( 'Search Portfolio Item', 'text_domain' ),
        'not_found'          => __( 'Not found', 'text_domain' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
    );

    $args = array(
        'label'               => __( 'portfolio', 'text_domain' ),
        'description'         => __( 'Portfolio Items', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => 'dashicons-portfolio', // Customize the icon
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    );

    register_post_type( 'portfolio', $args );
}

// Hook into the 'init' action
add_action( 'init', 'custom_portfolio_post_type', 0 );

This code registers custom post type named “Portfolio” with some basic settings. You can customize the labels, supports, and other parameters according to your specific needs.

Step 4: Customize Content Fields

To add content fields, modify the supports parameter in the $args array. For example, if you want to add a custom field named “Project Date,” you can modify the supports a line like this:

'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),

Step 5: Save and Test

Save the changes to your functions.php file. Now, go to your WordPress dashboard, and you should see a new menu item labeled “Portfolio.” Click on it, and you can start adding and managing your custom posts.

Congratulations! You’ve successfully created custom post type without using a plugin. This approach gives you greater control over your site’s structure and is an excellent way to tailor WordPress to your specific content needs. Remember to back up your files before making changes and test on a staging environment before implementing them on a live site.

Start your customization journey and enjoy the power of a uniquely tailored WordPress experience!

How to Duplicate Pages in WordPress by Plugin: A Simplified Guide

editor's pick

Create Custom Post Type

news via inbox

2 Comments

  1. […] Create a Custom Post Type in WordPress Without a Plugin […]

  2. […] Create a Custom Post Type in WordPress Without a Plugin […]

Leave A Comment