SlideShare a Scribd company logo
1 of 32
Download to read offline
K. Adam White, WordCamp Boston 2011

      Stepping Into Custom
      Post Types




Photo by dolanh
Introduction

        Who is this guy, anyway?

              K.Adam White

   Front-end developer & data architect
Introduction
Introduction
Custom Post Types
• What is a custom post type?
Custom Post Types
• What is a custom post type?
Custom Post Types
• What is a custom post type?

• Your own content type

• Blog Posts, Pages, Attachments, Revisions and
  Menus are all “post types” in WordPress 3.0
Register your post type
• Created in functions.php (or a plugin) using
  the “init” hook:
• add_action( 'init', 'create_post_type' );

  function create_post_type() {
     register_post_type(‘nameOfCPT’);
  }
A Basic Custom Post Type
• add_action( 'init', 'create_post_type' );

  function create_post_type() {
      register_post_type( 'acme_product',
              array(
                     'labels' => array(
                        'name' => __( 'Products' ),
                              'singular_name' => __( 'Product' )
                     ),
                     'public' => true,
                     'has_archive' => true,
              )
      );
  }
Basic Post Type Attributes
            • Names: How they display
              in the admin screens (left)
            • What fields the post type
              supports: the content
              editor, a title, thumbnails,
              comments, etc
            • Access rights
Default Template Fields
• Title, Content, Publish box
All Template Fields
             • Many more fields
               available out of the box
             • Once enabled, can be
               hidden or shown from
               the Screen Options tab
Go Crazy!
add_action('init', 'codex_custom_init');
function codex_custom_init()
{
  $labels = array(
   'name' => _x('Books', 'post type general name'), 'singular_name' => _x('Book', 'post type singular name'),
      'add_new' => _x('Add New', 'book'), 'add_new_item' => __('Add New Book'), 'edit_item' => __('Edit
      Book'), 'new_item' => __('New Book'), 'all_items' => __('All Books'), 'view_item' => __('View Book'),
      'search_items' => __('Search Books'), 'not_found' => __('No books found'), 'not_found_in_trash' => __('No
      books found in Trash'),
   'parent_item_colon' => '', 'menu_name' => 'Books‘

    );
    $args = array(
      'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true,
      'show_in_menu' => true,
      'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true,
      'hierarchical' => false, 'menu_position' => null, 'supports' =>
         array('title','editor','author','thumbnail','excerpt','comments')
    );
    register_post_type('book',$args);
}
But I need more…
• You can use the build-in Custom Fields to
  store custom data attributes
• You can extend the built-in template fields
  with add_meta_box() or a related plugin: I
  recommend WPAlchemy’s MetaBoxes
• Custom Taxonomies allow you create your
  own tags and categories, just for a particular
  Post type
But I’m not much of a coder…
• There’s a plugin for that
• Custom Post Type UI will let you create and
  configure a post type without touching
  functions.php
• Also supports custom taxonomies
Demo: Custom Post Type UI
How to display your Post Types
• Template Hierarchy:
Template Pages
• Create templates using the post type or
  taxonomy’s name:
  – single-{post_type}.php
  – archive-{post_type}.php
  – taxonomy-{taxonomy}.php
• e.g. if your post type is acme_products,
  single-acme_product.php will be used when
  you try to view a single product item
Using CPTs on other pages
From the WordPress Codex:

$args = array( 'post_type' => 'product',
  'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  the_content();
endwhile;
Case Study: Nutfield Technology
CPT #1: Home Page Items
Title, Content, Featured Image, and custom fields
Custom Fields
• Using WPAlchemy MetaBox
• Allows for easier content maintenance
CPT #2: Products
                   • Content, Title,
                     Featured Image,
                     File Attachments,
                     5 Taxonomies,
                     3 WPAlchemy
                     custom fields,
                     Gravity Forms,
                     and more
Custom Taxonomies in Action
• Terms used as classes, to allow easy JavaScript
  filtering of a product list
Advantages of Custom Posts
• Modularization of content on the homepage
• Better Admin panel experience for editors
• Taxonomies allowed quick development of a
  product selection application
• Allowed normal strengths of WordPress posts
  (featured images, file attachments) to be used
  on a custom template
Disadvantages
• Adding custom fields is still code-heavy and
  difficult
• No visual ‘template builder’ in WordPress
• Query syntax for custom posts can be tricky
Gotcha’s
1. Namespaces:

  Always prefix your post type names with a
  namespace, e.g.
  register_post_type(‘acme_products’);
  instead of just ‘products.’ This will minimize
  conflicts with other plugins.
Gotcha’s
2. Portability:

   If you change your theme, you will need to
   do a lot of work to make it compatible with
   your custom post types

   Locks you in to a particular theme (or theme
   framework)
Gotcha’s
3. SEO:

  To make the most of a custom post type, you
  should re-write their URLs using the
  ‘rewrite’ => array(‘slug’ => ‘seo-friendly-title’)
  parameter when creating the CPT. This will
  also hide your namespace from the public.
Gotcha’s
4. Easy to get carried away:

   Remember that another human will have to
   maintain these items! Design for simplicity. A
   more general data structure is usually the
   best.
References
•   Justin Tadlock’s Custom Post Types in WordPress
•   WordPress Codex Custom Post Types page
•   WPAlchemy custom MetaBox class
•   Example add_meta_box() plugin on WP Engineer
•   Custom Post Type UI plugin
Thank You!
• adam@kadamwhite.com
• +K.Adam
• @kadamwhite

• This is my first talk—Please rate me!
  http://speakerrate.com/talks/8024-stepping-
  into-custom-post-types

More Related Content

What's hot

Drupal overview-training
Drupal overview-trainingDrupal overview-training
Drupal overview-trainingSam Davis
 
What is the WordPress Loop?
What is the WordPress Loop?What is the WordPress Loop?
What is the WordPress Loop?webdesigncom
 
Creative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS BasicsCreative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS BasicsLukas Oppermann
 
Introduction to XMLUI and Mirage Theming for DSpace 3
Introduction to XMLUI and Mirage Theming for DSpace 3Introduction to XMLUI and Mirage Theming for DSpace 3
Introduction to XMLUI and Mirage Theming for DSpace 3Bram Luyten
 
Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Tarunsingh198
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017Amanda Giles
 

What's hot (7)

Drupal overview-training
Drupal overview-trainingDrupal overview-training
Drupal overview-training
 
What is the WordPress Loop?
What is the WordPress Loop?What is the WordPress Loop?
What is the WordPress Loop?
 
Creative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS BasicsCreative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS Basics
 
Introduction to XMLUI and Mirage Theming for DSpace 3
Introduction to XMLUI and Mirage Theming for DSpace 3Introduction to XMLUI and Mirage Theming for DSpace 3
Introduction to XMLUI and Mirage Theming for DSpace 3
 
Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Basic-CSS-tutorial
Basic-CSS-tutorialBasic-CSS-tutorial
Basic-CSS-tutorial
 

Viewers also liked

A/B Testing: Make your wildest conversion goals come true
A/B Testing: Make your wildest conversion goals come trueA/B Testing: Make your wildest conversion goals come true
A/B Testing: Make your wildest conversion goals come trueBill Robbins
 
Custom Post Types--Front End
Custom Post Types--Front EndCustom Post Types--Front End
Custom Post Types--Front EndBill Robbins
 
Advanced Custom Post Types
Advanced Custom Post TypesAdvanced Custom Post Types
Advanced Custom Post TypesAndy Stratton
 
WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015
WP 201   Custom Post Types - Custom Fields - WordCamp Columbus 2015WP 201   Custom Post Types - Custom Fields - WordCamp Columbus 2015
WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015Joe Querin
 
Custom Post Types in Depth at WordCamp Montreal
Custom Post Types in Depth at WordCamp MontrealCustom Post Types in Depth at WordCamp Montreal
Custom Post Types in Depth at WordCamp MontrealJoey Kudish
 
Custom Post Types and Taxonomies
Custom Post Types and TaxonomiesCustom Post Types and Taxonomies
Custom Post Types and TaxonomiesTammy Hart
 
Custom post types - WordPress
Custom post types - WordPressCustom post types - WordPress
Custom post types - WordPresskeithdevon
 

Viewers also liked (7)

A/B Testing: Make your wildest conversion goals come true
A/B Testing: Make your wildest conversion goals come trueA/B Testing: Make your wildest conversion goals come true
A/B Testing: Make your wildest conversion goals come true
 
Custom Post Types--Front End
Custom Post Types--Front EndCustom Post Types--Front End
Custom Post Types--Front End
 
Advanced Custom Post Types
Advanced Custom Post TypesAdvanced Custom Post Types
Advanced Custom Post Types
 
WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015
WP 201   Custom Post Types - Custom Fields - WordCamp Columbus 2015WP 201   Custom Post Types - Custom Fields - WordCamp Columbus 2015
WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015
 
Custom Post Types in Depth at WordCamp Montreal
Custom Post Types in Depth at WordCamp MontrealCustom Post Types in Depth at WordCamp Montreal
Custom Post Types in Depth at WordCamp Montreal
 
Custom Post Types and Taxonomies
Custom Post Types and TaxonomiesCustom Post Types and Taxonomies
Custom Post Types and Taxonomies
 
Custom post types - WordPress
Custom post types - WordPressCustom post types - WordPress
Custom post types - WordPress
 

Similar to Stepping Into Custom Post Types

Extending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post TypesExtending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post TypesUtsav Singh Rathour
 
WordPress 3 Custom Post Types
WordPress 3 Custom Post TypesWordPress 3 Custom Post Types
WordPress 3 Custom Post TypesDave Zille
 
Creatively creating custom post types! word sesh2
Creatively creating custom post types!  word sesh2Creatively creating custom post types!  word sesh2
Creatively creating custom post types! word sesh2techvoltz
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress WebsitesKyle Cearley
 
Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...
Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...
Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...Evan Mullins
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012Stephanie Leary
 
Wordcamp St. Louis - Clean Coding
Wordcamp St. Louis - Clean CodingWordcamp St. Louis - Clean Coding
Wordcamp St. Louis - Clean Codinginspector_fegter
 
Building themesfromscratchwithframeworks
Building themesfromscratchwithframeworksBuilding themesfromscratchwithframeworks
Building themesfromscratchwithframeworksDavid Brattoli
 
ITPROceed 2016 - The Art of PowerShell Toolmaking
ITPROceed 2016 - The Art of PowerShell ToolmakingITPROceed 2016 - The Art of PowerShell Toolmaking
ITPROceed 2016 - The Art of PowerShell ToolmakingKurt Roggen [BE]
 
Add-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyAdd-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his Dutyreedmaniac
 
Custom Post Types in WP3
Custom Post Types in WP3Custom Post Types in WP3
Custom Post Types in WP3gregghenry
 
WordPress A CMS for Beginners, Geeks and Those In-Between
WordPress A CMS for Beginners, Geeks and Those In-BetweenWordPress A CMS for Beginners, Geeks and Those In-Between
WordPress A CMS for Beginners, Geeks and Those In-BetweenHeidi Cool
 
Add-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyAdd-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyLeslie Doherty
 
Using Custom Post Types and Advanced Custom Fields with Elementor
 Using Custom Post Types and Advanced Custom Fields with Elementor Using Custom Post Types and Advanced Custom Fields with Elementor
Using Custom Post Types and Advanced Custom Fields with ElementorAngela Bowman
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)Stephanie Leary
 
WP 101 - Custom Fields & Post Types
WP 101 - Custom Fields & Post TypesWP 101 - Custom Fields & Post Types
WP 101 - Custom Fields & Post TypesJoe Querin
 

Similar to Stepping Into Custom Post Types (20)

Extending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post TypesExtending WordPress. Making use of Custom Post Types
Extending WordPress. Making use of Custom Post Types
 
WordPress 3 Custom Post Types
WordPress 3 Custom Post TypesWordPress 3 Custom Post Types
WordPress 3 Custom Post Types
 
Creatively creating custom post types! word sesh2
Creatively creating custom post types!  word sesh2Creatively creating custom post types!  word sesh2
Creatively creating custom post types! word sesh2
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...
Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...
Custom post types- Choose Your Own Adventure - WordCamp Atlanta 2014 - Evan M...
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012
 
Wordcamp St. Louis - Clean Coding
Wordcamp St. Louis - Clean CodingWordcamp St. Louis - Clean Coding
Wordcamp St. Louis - Clean Coding
 
Building themesfromscratchwithframeworks
Building themesfromscratchwithframeworksBuilding themesfromscratchwithframeworks
Building themesfromscratchwithframeworks
 
ITPROceed 2016 - The Art of PowerShell Toolmaking
ITPROceed 2016 - The Art of PowerShell ToolmakingITPROceed 2016 - The Art of PowerShell Toolmaking
ITPROceed 2016 - The Art of PowerShell Toolmaking
 
Add-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyAdd-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his Duty
 
presentation
presentationpresentation
presentation
 
Custom Post Types in WP3
Custom Post Types in WP3Custom Post Types in WP3
Custom Post Types in WP3
 
CodeIgniter & MVC
CodeIgniter & MVCCodeIgniter & MVC
CodeIgniter & MVC
 
WordPress A CMS for Beginners, Geeks and Those In-Between
WordPress A CMS for Beginners, Geeks and Those In-BetweenWordPress A CMS for Beginners, Geeks and Those In-Between
WordPress A CMS for Beginners, Geeks and Those In-Between
 
Add-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyAdd-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his Duty
 
presentation
presentationpresentation
presentation
 
Using Custom Post Types and Advanced Custom Fields with Elementor
 Using Custom Post Types and Advanced Custom Fields with Elementor Using Custom Post Types and Advanced Custom Fields with Elementor
Using Custom Post Types and Advanced Custom Fields with Elementor
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)
 
WP 101 - Custom Fields & Post Types
WP 101 - Custom Fields & Post TypesWP 101 - Custom Fields & Post Types
WP 101 - Custom Fields & Post Types
 

Recently uploaded

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 

Stepping Into Custom Post Types

  • 1. K. Adam White, WordCamp Boston 2011 Stepping Into Custom Post Types Photo by dolanh
  • 2. Introduction Who is this guy, anyway? K.Adam White Front-end developer & data architect
  • 5. Custom Post Types • What is a custom post type?
  • 6. Custom Post Types • What is a custom post type?
  • 7. Custom Post Types • What is a custom post type? • Your own content type • Blog Posts, Pages, Attachments, Revisions and Menus are all “post types” in WordPress 3.0
  • 8. Register your post type • Created in functions.php (or a plugin) using the “init” hook: • add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type(‘nameOfCPT’); }
  • 9. A Basic Custom Post Type • add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'acme_product', array( 'labels' => array( 'name' => __( 'Products' ), 'singular_name' => __( 'Product' ) ), 'public' => true, 'has_archive' => true, ) ); }
  • 10. Basic Post Type Attributes • Names: How they display in the admin screens (left) • What fields the post type supports: the content editor, a title, thumbnails, comments, etc • Access rights
  • 11. Default Template Fields • Title, Content, Publish box
  • 12. All Template Fields • Many more fields available out of the box • Once enabled, can be hidden or shown from the Screen Options tab
  • 13. Go Crazy! add_action('init', 'codex_custom_init'); function codex_custom_init() { $labels = array( 'name' => _x('Books', 'post type general name'), 'singular_name' => _x('Book', 'post type singular name'), 'add_new' => _x('Add New', 'book'), 'add_new_item' => __('Add New Book'), 'edit_item' => __('Edit Book'), 'new_item' => __('New Book'), 'all_items' => __('All Books'), 'view_item' => __('View Book'), 'search_items' => __('Search Books'), 'not_found' => __('No books found'), 'not_found_in_trash' => __('No books found in Trash'), 'parent_item_colon' => '', 'menu_name' => 'Books‘ ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','author','thumbnail','excerpt','comments') ); register_post_type('book',$args); }
  • 14. But I need more… • You can use the build-in Custom Fields to store custom data attributes • You can extend the built-in template fields with add_meta_box() or a related plugin: I recommend WPAlchemy’s MetaBoxes • Custom Taxonomies allow you create your own tags and categories, just for a particular Post type
  • 15. But I’m not much of a coder… • There’s a plugin for that • Custom Post Type UI will let you create and configure a post type without touching functions.php • Also supports custom taxonomies
  • 16. Demo: Custom Post Type UI
  • 17. How to display your Post Types • Template Hierarchy:
  • 18. Template Pages • Create templates using the post type or taxonomy’s name: – single-{post_type}.php – archive-{post_type}.php – taxonomy-{taxonomy}.php • e.g. if your post type is acme_products, single-acme_product.php will be used when you try to view a single product item
  • 19. Using CPTs on other pages From the WordPress Codex: $args = array( 'post_type' => 'product', 'posts_per_page' => 10 ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); the_title(); the_content(); endwhile;
  • 20. Case Study: Nutfield Technology
  • 21. CPT #1: Home Page Items Title, Content, Featured Image, and custom fields
  • 22. Custom Fields • Using WPAlchemy MetaBox • Allows for easier content maintenance
  • 23. CPT #2: Products • Content, Title, Featured Image, File Attachments, 5 Taxonomies, 3 WPAlchemy custom fields, Gravity Forms, and more
  • 24. Custom Taxonomies in Action • Terms used as classes, to allow easy JavaScript filtering of a product list
  • 25. Advantages of Custom Posts • Modularization of content on the homepage • Better Admin panel experience for editors • Taxonomies allowed quick development of a product selection application • Allowed normal strengths of WordPress posts (featured images, file attachments) to be used on a custom template
  • 26. Disadvantages • Adding custom fields is still code-heavy and difficult • No visual ‘template builder’ in WordPress • Query syntax for custom posts can be tricky
  • 27. Gotcha’s 1. Namespaces: Always prefix your post type names with a namespace, e.g. register_post_type(‘acme_products’); instead of just ‘products.’ This will minimize conflicts with other plugins.
  • 28. Gotcha’s 2. Portability: If you change your theme, you will need to do a lot of work to make it compatible with your custom post types Locks you in to a particular theme (or theme framework)
  • 29. Gotcha’s 3. SEO: To make the most of a custom post type, you should re-write their URLs using the ‘rewrite’ => array(‘slug’ => ‘seo-friendly-title’) parameter when creating the CPT. This will also hide your namespace from the public.
  • 30. Gotcha’s 4. Easy to get carried away: Remember that another human will have to maintain these items! Design for simplicity. A more general data structure is usually the best.
  • 31. References • Justin Tadlock’s Custom Post Types in WordPress • WordPress Codex Custom Post Types page • WPAlchemy custom MetaBox class • Example add_meta_box() plugin on WP Engineer • Custom Post Type UI plugin
  • 32. Thank You! • adam@kadamwhite.com • +K.Adam • @kadamwhite • This is my first talk—Please rate me! http://speakerrate.com/talks/8024-stepping- into-custom-post-types