0
WORDPRESS CUSTOM
POST TYPES
By Nile Flores
http://blondish.net
Objective
❏ Explain what custom post types are
❏ How to set up custom post types and
taxonomies with an example
❏ Share a ...
What are Custom Post Types?
Custom post types extend the functionality of
WordPress beyond just the default Post and Pages...
What Can Custom Post Types Do?
❏ Event Calendars
❏ Real Estate Listings
❏ Directories
❏ Portfolios
❏ Forums
❏ Testimonials...
The Scenario
I want to create a Portfolio. It has to:
❏ Be separate from posts and pages
❏ Have its own permalink structur...
In the beginning
In the functions.php of the theme, OR including a php file into
your theme for your custom post type, ins...
Wait!
You need to label it and make sure it is viewable. So here is the
modified code:
<?php
add_action( 'init', 'portfoli...
Are we done yet?
You need to label your arguments!!!!
❏ name: Name of your post type (plural)
❏ singular_name: Name of you...
So, now with labels:
Add support with meta boxes
Adding meta boxes to your site allows you to add areas to
your edit posts area so you can writ...
Once again…
Results of adding support
Permalinks!
You need to tell WordPress the URL structure for your
Custom Post Type. In the case of this presentation, we
w...
Taxonomies for Custom Post Types
Now that you have built the backend for your
custom post types, you need to add things li...
You Need To Pimp It!
In the case of your portfolio, you probably do not
want to use the same tag and category structure
as...
Custom Arguments
❏ hierarchical: determines if a post type has a hierarchy
❏ description: text description of custom post ...
So, yeah… taxonomies
By default, WordPress has several taxonomies:
❑ Category
❑ Link Category
❑ Tags
Like the WordPress co...
Adding Hierarchical Taxonomies
Hierarchical taxonomies are much like
categories.
The Basic Code
<?php
$arg= array( 'hierar...
Adding Non-Hierarchical Taxonomies
Non- hierarchical taxonomies are much like tags.
The Basic Code:
<?php
$arg= array( 'hi...
Taxonomies need arguments too!
❑ public: Determines if taxonomy is public
or not
❑ show_ui: Determines if taxonomy
display...
Remember Your Permalinks!
You definitely should be putting in the permalink
structure for your taxonomies in your custom p...
And Label Them Too!!!
Labels Continued…
Now For Your Theme
To display your custom post type on your site,
the following code MUST be inserted BEFORE
the loop for ...
Theming
Please note that you can use the same
templating system for single pages and
posts in your Custom Post Type otherw...
Example Custom Query
Say you just want to show 10 posts on a page for your custom post
type. Here is an example code:
<?ph...
Custom Post Type Plugin
Custom Post Type UI
http://wordpress.org/extend/plugins/custom-post-type-ui/
You don’t need to cod...
Custom Post Type Resources
❏ http://codex.wordpress.org/Custom_Post_Types
❏ http://codex.wordpress.org/Post_Types
❏ http:/...
Any
Questions?
Nile Flores
http://blondish.net
Twitter: @blondishnet
Facebook:
http://fb.com/NileFlores
http://slideshare....
Upcoming SlideShare
Loading in...5
×

WordPress Custom Post Types

8,771

Published on

WordCamp St. Louis 2012 Presentation

Published in: Technology, Business
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total Views
8,771
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
42
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "WordPress Custom Post Types"

  1. 1. WORDPRESS CUSTOM POST TYPES By Nile Flores http://blondish.net
  2. 2. Objective ❏ Explain what custom post types are ❏ How to set up custom post types and taxonomies with an example ❏ Share a brief walkthrough theme templating ❏ Share tools and resources to help make custom post types easier to understand
  3. 3. What are Custom Post Types? Custom post types extend the functionality of WordPress beyond just the default Post and Pages. By default, WordPress has the following post types: ❑Posts ❑Pages ❑Navigational Menus ❑Attachments ❑Revisions
  4. 4. What Can Custom Post Types Do? ❏ Event Calendars ❏ Real Estate Listings ❏ Directories ❏ Portfolios ❏ Forums ❏ Testimonials ❏ Quotes AND MORE!!!
  5. 5. The Scenario I want to create a Portfolio. It has to: ❏ Be separate from posts and pages ❏ Have its own permalink structure ❏ Be searchable Let’s just keep it simple for now. ;)
  6. 6. In the beginning In the functions.php of the theme, OR including a php file into your theme for your custom post type, insert the following code: <?php add_action( 'init', 'portfolio_register' ); function portfolio_register() { register_post_type( 'portfolio' ); } ?>
  7. 7. Wait! You need to label it and make sure it is viewable. So here is the modified code: <?php add_action( 'init', 'portfolio_register' ); function portfolio_register() { register_post_type( 'portfolio', $args ); $args = array( 'public' => true, 'label' => 'Portfolio' ); } ?>
  8. 8. Are we done yet? You need to label your arguments!!!! ❏ name: Name of your post type (plural) ❏ singular_name: Name of your post type (singular) ❏ add_new: Menu item for Add New Post ❏ add_new_item: Heading title when creating a new post ❏ edit_item: Heading title when editing a post ❏ new_item: Heading Title that is show in favorites menu in admin header ❏ view_item: Appears by permalink in the edit post area ❏ search_items: Appears in button text for search box in edit post area ❏ not_found: Text that displays when no post are found when searching ❏ not_found_in_trash: Text that appears when no posts are found in trash ❏ menu_name: Title that appears in the WordPress dashboard menu http://codex.wordpress.org/Function_Reference/register_post_type#Parameters
  9. 9. So, now with labels:
  10. 10. Add support with meta boxes Adding meta boxes to your site allows you to add areas to your edit posts area so you can write those new posts in your custom post types area. ❏title: Input field for post title ❏editor: Displays main content meta box ❏comments: Allows you to turn comments on or off ❏trackbacks: Allows you to turn trackbacks and pingbacks on or off ❏revisions: Allows revisions ❏author: Displays the author selection meta box ❏excerpt: Displays a textarea for writing custom excerpts ❏thumbnail: Adds meta box for post thumbnail (aka featured image) ❏custom-fields: Displays custom fields area ❏page-attributes: Displays box for pages to allow selecting parent post ❏post-formats: Add post formats
  11. 11. Once again…
  12. 12. Results of adding support
  13. 13. Permalinks! You need to tell WordPress the URL structure for your Custom Post Type. In the case of this presentation, we want our URL structure to be like: yourdomain. com/portfolio Add the following to do this: 'rewrite' => array( 'slug' => 'portfolio' ),
  14. 14. Taxonomies for Custom Post Types Now that you have built the backend for your custom post types, you need to add things like categories and tags. These are commonly called taxonomies. Default taxonomies are called as followed: 'taxonomies' => array( 'post_tag', 'category' );
  15. 15. You Need To Pimp It! In the case of your portfolio, you probably do not want to use the same tag and category structure as the default post types in WordPress, so you need to create them.
  16. 16. Custom Arguments ❏ hierarchical: determines if a post type has a hierarchy ❏ description: text description of custom post type ❏ show_ui: Determines if you want to show the admin menu or screens ❏ menu_position: This sets the position of the custom post type in the WordPress dashboard menu ❏ menu_icon: URL to custom menu icon ❏ exclude_from_search: Determines if content should display in search results ❏ can_export: Determines if the post type can be exported ❏ show_in_menu: Determines if the post type can be shown in an existing admin menu ❏ has_archive: Enables post types archives
  17. 17. So, yeah… taxonomies By default, WordPress has several taxonomies: ❑ Category ❑ Link Category ❑ Tags Like the WordPress codex says – Taxonomies are a way to group items together http://codex.wordpress.org/Taxonomies
  18. 18. Adding Hierarchical Taxonomies Hierarchical taxonomies are much like categories. The Basic Code <?php $arg= array( 'hierarchical' => true, 'label' => 'Project Type‘ ); register_taxonomy( 'project type', 'portfolio', $arg ); ?>
  19. 19. Adding Non-Hierarchical Taxonomies Non- hierarchical taxonomies are much like tags. The Basic Code: <?php $arg= array( 'hierarchical' => false, 'label' => 'Project Tags’); register_taxonomy( 'project tags', 'portfolio', $arg ); ?>
  20. 20. Taxonomies need arguments too! ❑ public: Determines if taxonomy is public or not ❑ show_ui: Determines if taxonomy displays in admin menu ❑ query_var: Determines if taxonomy can be queried ❑ show_tagcloud: Determines whether to show tagcloud in admin ❑ show_in_nav_menus: Determines whether taxonomy will be selectable in menus
  21. 21. Remember Your Permalinks! You definitely should be putting in the permalink structure for your taxonomies in your custom post types. In the case of this presentation, add the following labels: Categories - 'rewrite' => array( 'slug' => ‘type-of- project' ), Tags - 'rewrite' => array( 'slug' => ‘project-tags' ),
  22. 22. And Label Them Too!!!
  23. 23. Labels Continued…
  24. 24. Now For Your Theme To display your custom post type on your site, the following code MUST be inserted BEFORE the loop for your archives. Usually this template would be called archive-portfolio.php (in the case of this presentation.) <?php query_posts( array( 'post_type' => 'portfolio' ) ); ?>
  25. 25. Theming Please note that you can use the same templating system for single pages and posts in your Custom Post Type otherwise will use the most basic template. In the case of this presentation, if you do not have single-portfolio.php, it will either look for index-portfolio.php. If that does not exist, it will look even to your index. php template file. You do not have to put the Custom Post Type Query as shown in the previous slide into these templates unless you are doing a custom query.
  26. 26. Example Custom Query Say you just want to show 10 posts on a page for your custom post type. Here is an example code: <?php $portfolio = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => 10 ) ); while ($portfolio->have_posts() ): $portfolio->the_post(); the_title('<h2><a href="" . get_permalink() . "">', '</a></h2>'); ?> <div class="entry"> <?php the_content(); ?> </div> <?php endwhile; ?>
  27. 27. Custom Post Type Plugin Custom Post Type UI http://wordpress.org/extend/plugins/custom-post-type-ui/ You don’t need to code if you use this plugin. OR You can use it as a guide.
  28. 28. Custom Post Type Resources ❏ http://codex.wordpress.org/Custom_Post_Types ❏ http://codex.wordpress.org/Post_Types ❏ http://codex.wordpress.org/Function_Reference/get_the_term_list ❏ http://codex.wordpress.org/Function_Reference/register_post_type ❏ http://justintadlock.com/archives/2010/04/29/custom-post-types-in- wordpress/ ❏ http://justintadlock.com/archives/2010/02/02/showing-custom-post- types-on-your-home-blog-page/ ❏ http://kovshenin.com/archives/custom-post-types-in-wordpress-3-0/ ❏ http://www.wpbeginner.com/wp-tutorials/how-to-use-custom-post- types/ ❏ http://yoast.com/custom-post-type-snippets/
  29. 29. Any Questions? Nile Flores http://blondish.net Twitter: @blondishnet Facebook: http://fb.com/NileFlores http://slideshare.net/blondishnet
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×