3

I'm not sure what this feature is called, but I've seen examples of this before. Lets say, on my site I want additional collections of things (besides blog posts) that have their own sections in the admin area.

For example, I have:

  • a list of events
  • a list of products
  • a list of testimonials

...and they each need their own page to list them.

How does this work in WordPress and how can I create my own?

  • 1
    You mean custom taxonomy terms and their archive pages for listing and categorising, and custom post types? – Tom J Nowell Apr 27 '15 at 20:32
  • 1
    Your requirement can be fulfilled with Custom Post Type UI and Custom Post Type List Shortcode plugins. – Vikram Singh Apr 29 '15 at 12:55
14
+50

I believe what you're referring to is called "Post Types". By default, WordPress has a complete post types you're already familiar with - post ( news / blog ), page ( Pages ), attachment ( Media ), revision ( Page / Post Revisions ). You can read up on the full list of built in post types on The Codex - Post Types.

If you want to register a new Custom Post Type ( CPT ) you can do pretty easily with some PHP knowledge. Let's go through and create our own post type called "Products".

Step One - Connect to FTP - you do not want to do this using Appearance -> Editor.

Step Two - Locate your theme, if you're using a child theme open that up and locate the functions.php file. Open the file in Notepad++ or your editor of choice.

Step Three - Register Your Post Type

Toward the top of your functions.php file we're going to hook into WordPress init and use a built-in WordPress function cleverly called: register_post_type. I highly suggest you read these codex links as I will not explain each point but instead give a brief description.

/** Create Post Type **/
function cpt_init() {

    // Products Custom Post Type
    register_post_type( 'cpt_products', array(                  // Internal name `cpt_products` - this is what we test against in queries
        'labels'            =>  array(                          // An array of Administrative labels
            'name'          =>      __( 'Products' ),
            'singular_name' =>      __( 'Product' ),
            'all_items'     =>      __( 'View Products' ),
            'add_new'       =>      __( 'New Product' ),
            'add_new_item'  =>      __( 'New Product' ),
            'edit_item'     =>      __( 'Edit Product' ),
            'view_item'     =>      __( 'View Product' ),
            'search_items'  =>      __( 'Search Products' ),
            'no_found'      =>      __( 'No Products Found' ),
            'not_found_in_trash' => __( 'No Products in Trash' )
        ),
        'public'            =>  true,                           // Whether it will be publically available or only in the admin panel
        'publicly_queryable'=>  true,                           // "                                                                "
        'show_ui'           =>  true,                           // Whether we want this post type to show up in the admin panel
        'show_in_nav_menus' =>  false,                          // If we want Products to show up in the `Appearance -> Menu`
        'capability_type'   =>  'page',                         // Mostly used for user capability
        'hierarchical'      =>  false,                          // If you will allow products to have child products / assigned a parent
        'rewrite'           =>  array( 'slug' => 'product', 'with_front' => false ),        // This is going to be what the single post will be located at `/product/product-name/`
        'menu_icon'         =>  'dashicons-cart',               // https://developer.wordpress.org/resource/dashicons/#welcome-widgets-menus
        'menu_position'     =>  5,                              // Where you want it to show up in the admin panel
        'supports'          =>  array( 'title', 'editor', 'page-attributes', 'revisions' )  // What is actually shown when a new product is added - admin panel
    ) );
}
add_action( 'init', 'cpt_init' );

Step Four - Upload functions.php - an important note here is to also flush permalinks which is as easy as going to Settings -> Permalinks and click "Save" button at the bottom of the page.


Similar to creating a post type, if you want to create Categories ( similar to Post Categories ) you can do so using the register_taxonomy function. Follow the steps as above until Step 3.

Step 3 - Register Taxonomy

The first two parameters are important to link the taxonomy to the post type. The next important part is the hierarchical parameter - this will be the difference between post_tags ( false ) and categories ( true ). We will make this taxonomy hierarchical which is similar to post categories.

/** Add Custom Taxonomy **/
function tax_init() {
    // Product Categories
    register_taxonomy( 
        'tax_products',         // Taxonomy slug
        'cpt_products',         // What post type we're assigning the taxonomy to.
        array(                  // Array of Arguments
            'labels'            =>  array(                                  // User Friendly Labels
                'name'              => __( 'Product Categories' ),
                'singular_name'     => __( 'Product Category' ),
                'search_items'      => __( 'Search Product Categories' ),
                'all_items'         => __( 'All Product Categories' ),
                'parent_item'       => __( 'Parent Product Category' ),
                'parent_item_colon' => __( 'Parent Product Category:' ),
                'edit_item'         => __( 'Edit Product Category' ), 
                'update_item'       => __( 'Update Product Category' ),
                'add_new_item'      => __( 'Add New Product Category' ),
                'new_item_name'     => __( 'New Product Category' ),
                'menu_name'         => __( 'Product Categories' )
            ),
            'public'            =>  true,               // Whether it will be public or only used in the admin panel
            'hierarchical'      =>  true,               // The differnce between *Tags* and *Categories*
            'show_in_nav_menus' =>  true,               // To show up in `Appearance -> Menus` or not.
            'show_admin_column' =>  true,               // If we want an column next to the title when listing our products
            'rewrite'           =>  array( 'slug' => 'products/category', 'with_front' => false, 'hierarchical' => true )       // Similar to post type, where to find the categories `products/category/category-1/`
        ) 
    );
}
add_action( 'init', 'tax_init');
  • 3
    This site automatically generates the CPT code for you. Very handy :) – BenFreke Apr 30 '15 at 0:40
  • @BenFreke that whole website looks pretty nifty, good find! – Howdy_McGee Apr 30 '15 at 1:00
0

These are called custom post types. There are several ways to create new post type, using a plugin or creating yourself.

function codex_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Books'
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );

This will add a new type of content(post) called Book.

See codex page for details -https://codex.wordpress.org/Function_Reference/register_post_type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.