WordPress Custom Post Type Code

Posted in: - Feb 19, 2011 6 Comments
// custom post type code from functions.php
// I created a custom post type 'code-snippet'
// with taxonomy 'technology'

add_action( 'init', 'create_code_snippets' );

function create_code_snippets() {
	  $labels = array(
		'name' => _x('Code Snippets', 'post type general name'),
		'singular_name' => _x('Code Snippet', 'post type singular name'),
		'add_new' => _x('Add New', 'Code Snippet'),
		'add_new_item' => __('Add New Code Snippet'),
		'edit_item' => __('Edit Code Snippet'),
		'new_item' => __('New Code Snippet'),
		'view_item' => __('View Code Snippet'),
		'search_items' => __('Search Code Snippets'),
		'not_found' =>  __('No Code Snippets found'),
		'not_found_in_trash' => __('No Code Snippets found in Trash'),
		'parent_item_colon' => ''
	  );

	  $supports = array(
                         'title',
                         'editor',
                         'author',
                         'custom-fields',
                         'revisions',
                         'excerpt',
                         'thumbnail',
                         'comments',
                         'trackbacks'
                         );
  // wordpress function to register post type
  // http://codex.wordpress.org/Function_Reference/register_post_type
	  register_post_type( 'code-snippet',
		array(
		  'labels' => $labels,
		  'public' => true,
	   	  'menu_position' => 5,
		  'supports' => $supports
		)
	  );
}

// to build taxonomy 'technology' for custom post type 'code-snippet'
add_action( 'init', 'build_taxonomies', 0 );  

function build_taxonomies() {
	register_taxonomy(
                'technology',
                'code-snippet',
                array(
                    'hierarchical' => false,
                    'label' => 'Technology',
                    'query_var' => true,
                    'rewrite' => true
                )
        );
}

Use the following code in your template page to create a custom loop that displays code snippets.

query_posts("post_type=code-snippet");

I hope this will help.
Thanks

6 Responses

  1. comment writer
    Reply David Radovanovic says:

    Saqib, Would you happen to have a example/demo to take a peek at? Thanks for sharing.

  2. comment writer
    Reply Toure says:

    HEllo, I am creating a gallery of products (shoes, clothes, t-shirts, etc…) I was wondering how can I reduce coding by implementing an arrays in registering and defining the products since all will have the same properties via the main categories. I think it is a waste of time to individually register and define products one by one.

    • comment writer
      Reply saqib sarwar says:

      Thanks for your comment @Toure, I think it would be better to define just one custom post type for products and classify different products types using custom taxonomies. Google about custom taxonomies and you will know what I am saying.

      • comment writer
        Toure says:

        I tried, but I found redundant to do that since all of the products have the same taxonomies (model for men-women-kids, size, brand). i found it easy and cleaner to use the built-in category et only use custom post type to separate different kind of products like shoes, dresses, hoodies, etc…
        I hope there might better ways of doing it without cluttering the functions.php file.
        It even easier to query the built-in categories.
        Any body, please guide me in the right direction.
        Thanks

  3. comment writer
    Reply saqib sarwar says:

    @Tour. can you send me your function.php so that I can have a look on what exactly you are doing ?

Trackbacks

Leave a Reply