You are gonna want to grab this plugin Easy Content Types
Here’s a nice zip file of it. easy-content-types
Chicken Meta
<?php $meta_values = get_post_meta($post->ID); ?>
<ul>
<li><strong>Temperament:</strong> <?php echo $meta_values['ecpt_temperament'][0]; ?></li>
<li><strong>Egg Yield:</strong> <?php echo $meta_values['ecpt_typicaleggyield'][0]; ?></li>
<li><strong>Additional Notes:</strong> <?php echo $meta_values['ecpt_additionalnotes'][0]; ?></li>
</ul> |
Single Chickens
<?php get_header(); ?>
test
<!-- Row for main content area -->
<div class="small-12 large-8 columns" role="main">
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php reverie_entry_meta(); ?>
</header>
<div class="entry-content">
<?php the_content(); ?>
</div>
<footer>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'reverie'), 'after' => '</p></nav>' )); ?>
<p><?php the_tags(); ?></p>
</footer>
<?php comments_template(); ?>
</article>
<?php endwhile; // End the loop ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?> |
What the hell is a custom post type anyway?
A Custom Type is a Post Type you define. http://codex.wordpress.org/Post_Types#Custom_Types. Custom Post Types became part of the core in WordPress 3.0.
Potential Custom Post Types
- Real estate listings.
- Event calendar (I know a lot of folks are interested in this).
- Movie Database
- Book Database
- A forum without a lot of integration problems.
- A ticket system like the WordPress Trac.
- Design gallery/showcase.
- Teams
Adding a custom type to WordPress is done via the register_post_type function. This function allows you to define the post type and how it operates within WordPress.
Here’s a basic example of adding a custom post type:
1
2
3
4
5
6
7
8
9
10
11
12
13
| 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,
)
);
} |
Labels
The above example uses 2 labels – but many more are available.
- name: The plural form of the name of your post type.
- singular_name: The singular form of the name of your post type.
- add_new: The menu item for adding a new post.
- add_new_item: The header shown when creating a new post.
- edit: The menu item for editing posts.
- edit_item: The header shown when editing a post.
- new_item: Shown in the favorites menu in the admin header.
- view: Used as text in a link to view the post.
- view_item: Shown alongside the permalink on the edit post screen.
- search_items: Button text for the search box on the edit posts screen.
- not_found: Text to display when no posts are found through search in the admin.
- not_found_in_trash: Text to display when no posts are in the trash.
- parent: Used as a label for a parent post on the edit posts screen. Only useful for hierarchical post types.
with full arguments it’d look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| 'labels' => array(
'name' => __( 'Super Dupers' ),
'singular_name' => __( 'Super Duper' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Super Duper' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Super Duper' ),
'new_item' => __( 'New Super Duper' ),
'view' => __( 'View Super Duper' ),
'view_item' => __( 'View Super Duper' ),
'search_items' => __( 'Search Super Dupers' ),
'not_found' => __( 'No super dupers found' ),
'not_found_in_trash' => __( 'No super dupers found in Trash' ),
'parent' => __( 'Parent Super Duper' ),
), |
The public argument
catchall argument for several other arguments and defaults to false. Depending on whether it’s set to true or false, it’ll automatically decide what other arguments should be unless they’re specifically defined. If you’re looking for finer control over the public arguments, there are three specific arguments you may set:
- show_ui: Whether to show the administration screens.
- publicly_queryable: Whether queries for this post type can be performed from the front end.
- exclude_from_search: Whether the posts should appear in search results.
1
2
3
4
| 'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false, |
Menu Position
By default, a new post type is added after the Comments menu item in the admin. But, you have to ability to move it to a position more suitable for you. Default WordPress menu items are set apart by integrals of 5. For example, using 20 will add your menu item after Pages.
1
| 'menu_position' => 20, |
Menu Icon
New post types will default to the Posts menu icon, but if you want to mix it up a bit or give your post type some separation from other elements, you can define a custom icon. You only have to input a a custom URL to an image file.
1
| 'menu_icon' => get_stylesheet_directory_uri() . '/images/super-duper.png', |
Hierarchical
The hierarchical argument allows you to choose whether you want your post type to be hierarchical. It defaults to false. If you set it to true, your posts will behave like pages in WordPress.
1
| 'hierarchical' => true, |
Query Var
The query_var argument allows you to control the query variable used to get posts of this type. For example, you could use it with the query_posts() function or WP_Query class. This will default to the name of your taxonomy.
1
| 'query_var' => true, |
Supports
The supports argument allows you to define what meta boxes and other fields will appear on the screen when editing or creating a new post. This defaults to title and editor. There are several available options:
- title: Text input field to create a post title.
- editor: Content input box for writing.
- comments: Ability to turn comments on/off.
- trackbacks: Ability to turn trackbacks and pingbacks on/off.
- revisions: Allows revisions to be made of your post.
- author: Displays a select box for changing the post author.
- excerpt: A textarea for writing a custom excerpt.
- thumbnail: The thumbnail (featured image in 3.0) uploading box.
- custom-fields: Custom fields input area.
- page-attributes: The attributes box shown for pages. This is important for hierarchical post types, so you can select the parent post.
1
| 'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail' ), |
Rewrite
The rewrite argument allows you to define the permalink structure
- slug: The slug you’d like to prefix your posts with.
- with_front: Whether your post type should use the front base from your permalink settings (for example, if you prefixed your structure with /blog or /archives).
1
| 'rewrite' => array( 'slug' => 'cool', 'with_front' => false ), |
Taxonomies
If you have some preexisting taxonomies (or want to create some) you can allow posts of this type to also use those taxonomies. You just have to set an array of taxonomy names that you’d like for it to use.
1
| 'taxonomies' => array( 'post_tag', 'category '), |
Can Export
You can use the can_export argument to decide whether posts of your post type can be exportable via the WordPress export tool.
1
| 'can_export' => true, |
Custom Taxonomies
This is a great post – so we’re just gonna use it.
http://wp.smashingmagazine.com/2012/01/04/create-custom-taxonomies-wordpress/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| function player_taxonomy() {
$labels = array(
'name' => _x( 'Team', 'taxonomy general name' ),
'singular_name' => _x( 'Team', 'taxonomy singular name' ),
'search_items' => __( 'Search Teams' ),
'all_items' => __( 'All Teams' ),
'parent_item' => __( 'Parent Team' ),
'parent_item_colon' => __( 'Parent Team:' ),
'edit_item' => __( 'Edit Team' ),
'update_item' => __( 'Update Team' ),
'add_new_item' => __( 'Add New Team' ),
'new_item_name' => __( 'New Team Name' ),
'menu_name' => __( 'Team' ),
);
register_taxonomy(
'leagues',
'players',
array(
'hierarchical' => true,
'labels' => $labels,
'query_var' => true,
'rewrite' => array('slug' => 'leagues')
)
);
}
add_action( 'init', 'player_taxonomy' );
add_action( 'init', 'players' );
function players() {
register_post_type( 'players', array(
'labels' => array(
'name' => __( 'Players' ),
'singular_name' => __( 'Player' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Player' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Player' ),
'new_item' => __( 'New Player' ),
'view' => __( 'View Players' ),
'view_item' => __( 'View Player' ),
'search_items' => __( 'Search Players' ),
'not_found' => __( 'No Players found' ),
'not_found_in_trash' => __( 'No Players found in Trash' ),
'parent' => __( 'Players' ),
),
'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'supports' => array('title'/*,'editor','page-attributes','custom-fields','thumbnail','excerpt','comments'*/),
'show_ui' => true,
'rewrite' => array(
'slug' => 'players',
'with_front' => false
),
'has_archive' => 'players',
'taxonomies' => array('leagues')
) );
} |
How to Query Custom Post Types
1
2
3
4
5
| $args = array( 'post_type' => 'product', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo ' |
‘; the_content(); echo ‘
Useful Links
http://themergency.com/generators/wordpress-custom-post-types/
http://wordpress.org/extend/plugins/custom-post-type-ui/
Recent Comments