Create a Custom Post Type in Your WordPress Theme

In order to create and use custom post types on your WordPress website, you need to be sure that your WordPress theme contains the correct code and functions. In the following steps, you create a very basic and generic custom post type called “Generic Content,” and you also find detailed information on the different parameters you can use with custom post types to create different types to suit your needs:



  1. In your Dashboard, choose Appearance→Editor link.


    The Edit Themes page opens.



  2. Click the Theme Functions template link to open the functions.php file.


    The Theme Functions template opens in the text editor in the middle of the page.



  3. Add the custom post type code to the bottom of the theme functions template file.


    Scroll down to the bottom of the functions.php file and include the following code to add a Generic Content custom post type to your site:


    // ADD CUSTOM POST TYPE
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    register_post_type( 'generic-content',
    array(
    'labels' => array(
    'name' => __( 'Generic Content' ),
    'singular_name' => __( 'Generic Content' )
    ),
    'public' => true
    )
    );
    }

    The function register_post_type(); can accept several arguments and parameters, which is detailed in the table. You can use a variety and a combination of arguments and parameters to create a specific post type. You can find more information on custom post types and using the register_post_type(); function on the WordPress Codex page.



  4. Click the Update File button to save the changes made to the functions.php file.
























































































Arguments and Parameters for register_post_types();
ParameterInformationParametersExample
labelThe name of the post typeNone'label' => __( 'Generic Content' ),
singular_labelSame as label, but in singular, not plural format (Posts become
Post, for example)
None'singular_label' => __( 'Generic Content' ),
descriptionThe description of the post type; displayed in the Dashboard to
represent the post type
None'description' => __( 'This is a description of the Generic
Content type' ),
public



show_ui



publicly_queryable



exclude_from_search
public sets whether the post type is
public.

show_ui either shows admin screens or
doesn’t.

publicly_queryable allows this post type
to be included in public queries within template code.

exclude_from_search either shows post
type in search results or doesn’t.
true or class="code">false (default)'public' => true,

'show_ui' => true,

'publicly_queryable' => true,

'exclude_from_search' => false,
menu_positionSets the position of the post type menu item in the Dashboard
navigation menu; by default, custom post types appear after the
Comments menu in the Dashboard.
Default: 20; sets integers in intervals of 5'menu_position' => 25,
menu_iconDefines a custom icon (or graphic) to the post type menu item
in the Dashboard navigation menu; creates and uploads the image
into the images directory of your theme
folder
None'menu_icon' => get_stylesheet_directory_uri() .
'/images/generic-content.png',
hierarchicalTells WordPress whether to display the post type content list
in a hierarchical manner
true or class="code">false; default is class="code">true'hierarchical' => true,
query_varControls whether this post type can be used with a class="code">query variable, such as class="code">query_posts true or class="code">false; default'query_var' => true,
capability_typeDefines permissions for users to edit, create, or read the
custom post type
post (default); gives the same
capabilities for those who can edit, create, and read blog
posts
'query_var' => post,
supportsDefines what meta boxes, or modules, are available for
this post type in the Dashboard
title (text box for title); class="code">editor (text box for content); class="code">comments (check boxes to toggle comments);
trackbacks (check boxes to toggle
trackbacks); revisions (allows
revisions); author (drop-down box to
define author); excerpt (text box for
excerpt); thumbnail (featured image
selection); custom-fields (custom fields
input area); page-attributes (page parent
and Page template drop-down lists)
'supports' => array( 'title', 'editor', 'excerpt',
'custom-fields', 'thumbnail' ),
rewriteRewrites the permalink structure for the post typetrue or class="code">false; slug
(permalink slug to prepend custom post types); class="code">with_front (if you’ve set permalink
structure with a specific prefix)
'rewrite' => array( 'slug' => 'my-content', 'with_front'
=> false ),
taxonomiesUses existing WordPress taxonomies (category and tag)Category post_tag'taxonomies' => array( 'post_tag', 'category'),
<Tip>

If you don’t feel up to writing all this code in the theme functions file, the Custom Post Type UI plugin from WebDevStudios provides you with an easy interface in your WordPress Dashboard. This plugin also simplifies the creation of custom post types on your site and bypasses the need to create the code in the Theme Functions (functions.php) file. You can find the free plugin.




dummies

Source:http://www.dummies.com/how-to/content/create-a-custom-post-type-in-your-wordpress-theme.html

No comments:

Post a Comment