WordPress custom post types are very handy, if you want an extra section on your blog that is about a different subject or contains different content then this is for you. So lets say you have a business website that contains information about your business, and you want to show the products that you offer in a separate section of the site. So all posts contained within the “Products” custom post type will be found under http://domain.com/products/example

Creating a custom post type in Wordpres is very easy. Just replace the word “products” with your desired custom post type name and copy & paste into your functions.php file…

[php]
add_action( ‘init’, ‘create_post_type’ );
function create_post_type() {
register_post_type( ‘products’,
array(
‘labels’ => array(
‘name’ => __( ‘Products’ ),
‘singular_name’ => __( ‘Products’ ),
),
‘menu_position’ => 4,
‘rewrite’ => array(‘slug’=>”,’with_front’=>false),
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘custom-fields’, ‘thumbnail’ ),
)
);
}
[/php]

This code enables support for: excerpt, custom-fields, thumbnails

If you want to have multiple post types your could should look something like this…

[php]
function create_post_type() {
register_post_type( ‘products’,
array(
‘labels’ => array(
‘name’ => __( ‘Products’ ),
‘singular_name’ => __( ‘Products’ ),
),
‘menu_position’ => 4,
‘rewrite’ => array(‘slug’=>”,’with_front’=>false),
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘custom-fields’, ‘thumbnail’ ),
)
);
register_post_type( ‘testimonials’,
array(
‘labels’ => array(
‘name’ => __( ‘Testimonials’ ),
‘singular_name’ => __( ‘Testimonials’ ),
),
‘menu_position’ => 5,
‘rewrite’ => array(‘slug’=>”,’with_front’=>false),
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘comments’, ‘excerpt’, ‘custom-fields’, ‘thumbnail’ ),
)
);
}
[/php]

Also if you wanted to add support for Tags and Categories, add the following code below…

[php]
add_action(‘init’, ‘add_default_boxes’);
function add_default_boxes() {
register_taxonomy_for_object_type(‘category’, ‘products’);
register_taxonomy_for_object_type(‘category’, ‘testimonials’);
register_taxonomy_for_object_type(‘post_tag’, ‘products’);
register_taxonomy_for_object_type(‘post_tag’, ‘testimonials’);
}
function my_post_types( $post_types ) {
$post_types[] = ‘products';
$post_types[] = ‘testimonials';
return $post_types;
}
add_filter( ‘cpt_post_types’, ‘my_post_types’ );
add_filter(‘pre_get_posts’, ‘query_post_type’);
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var(‘post_type’);
if($post_type)
$post_type = $post_type;
else
$post_type = array(‘products’, ‘testimonials’);
$query->set(‘post_type’,$post_type);
return $query;
}
}
[/php]

Related Terms:
Posted on 14th December 2012 by in Web Design Hampshire, Web Design Portsmouth, Web Design Southampton, Web Design Tips, Wordpress
  • Facebook Application Directory

    Good information about custom code.

Web Agency Portsmouth
where to go from here? get in touch OR

give us a call on 02392 123 538

Let's be friends! More...
Facebook Twitter Google+
Like & Follow Facebook Twitter Google+
Share the love, share this page! Facebook Twitter Google Digg Reddit LinkedIn Pinterest Email
Close [X]
Web Design South Coast
Areas We Cover

Here are some of the local areas we provide with web design and digital marketing services. These are just some examples, we work with clients all over the UK and across the globe.

Our Services
Close [X]
The Web Taylor
Harcourt Close Waterlooville, Hampshire PO8 8JL
02392 123358