wordpress custom post types
DESCRIPTION
WordCamp St. Louis 2012 PresentationTRANSCRIPT
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 brief walkthrough theme templating Share tools and resources to help make
custom post types easier to understand
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
What Can Custom Post Types Do?
Event Calendars Real Estate Listings Directories Portfolios Forums Testimonials Quotes
AND MORE!!!
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. ;)
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:
<?phpadd_action( 'init', 'portfolio_register' );function portfolio_register() {
register_post_type( 'portfolio' );
}?>
Wait!
You need to label it and make sure it is viewable. So here is the modified code:
<?phpadd_action( 'init', 'portfolio_register' );function portfolio_register() {
register_post_type( 'portfolio', $args );
$args = array( 'public' => true,'label' => 'Portfolio'
);}?>
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
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 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
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 want our URL structure to be like: yourdomain.com/portfolio
Add the following to do this:'rewrite' => array( 'slug' => 'portfolio' ),
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' )
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.
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
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
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 );
?>
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 );
?>
Taxonomies need arguments too!
public: Determines if taxonomy is public or not
show_ui: Determines if taxonomy displays in admin menu
query_var: Deterrmines if taxonomy can be queried
show_tagcloud: Determines whether to show tagcloud in admin
show_in_nav_menus: Determies whether taxonomy will be selectable in menus
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' ),
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 your archives. Usually this template would be called archive-portfolio.php (in the case of this presentation.)
<?php query_posts( array( 'post_type' => 'portfolio' ) ); ?>
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.
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; ?>
Custom Post Type Plugin
Custom Post Type UIhttp://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.
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/
Any Questions?
Thank you!!!
Nile Flores
http://blondish.net
Twitter: @blondishnet
Facebook:
http://fb.com/NileFlores
http://slideshare.net/blondishnet