Custom Post Type – Category – Tag For Custom Post Type
1.Declare a Custom Post Type
You how to use wordpress for a long time will surely see in the wordpress admin manager in management sometimes up to 2 parts, one of the post is one of the product, which is common in the sales theme. Usually distinguish two separate parts, one part specialized to upload product, one specialist to upload all know, news.
to declare the custom post type you create a form similar to the following code in your functions.php file:
add_action('init', 'create_product_post_type');
function create_product_post_type(){
register_post_type('product',
array(
'labels' => array(
'name' => __('Product'),
'singular_name' => __('Product'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Product'),
'edit' => __('Edit'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view' => __('View Product'),
'view_item' => __('View Product'),
'search_items' => __('Search Products'),
'not_found' => __('No Products found'),
'not_found_in_trash' => __('No Products found in Trash')
),
'public' => true,
'show_ui' => true,
'publicy_queryable' => true,
'exclude_from_search' => false,
'menu_position' => 20,
'menu_icon' => get_stylesheet_directory_uri(). '/images/product.png',
'hierarchical' => false,
'query_var' => true,
'supports' => array(
'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail',
'custom-fields'
),
'rewrite' => array('slug' => 'product', 'with_front' => false),
//'taxonomies' => array('post_tag', 'category'),
'can_export' => true,
//'register_meta_box_cb' => 'call_to_function_do_something',
'description' => __('Product description here.')
)
);
Looking at the above code you can understand somewhat, right, I’ll briefly explain the structure of code.
The most important is the content of the function “function create_product_post_type ()”, you change the wording to suit your purposes.
labels: is the corresponding text display functions for custom post type, look here, you know what I should do right, this is the standard code I think you do not need to change anything.
publicy_queryable: Allow query the Product query_posts function ().
menu_position: select location appears chomenu products, number 20 corresponds to the number 20 position in the menu order, you will see the different changes of position. 20 is positioned after the Management Pages
menu_icon: address is the path of the icon size is 16×16.
hierarchical: get parent hierarchy or not
query_var: variables used in the Get method.
supports: meta declare box as author, comment …
rewrite: create a link with wrong doing query
can_export: can export the contents of the XML file
description: interpretation.
2. Add custom taxonomy Products
To register the custom taxonomy for post type product, code similar to the following structure:
add_action('init', 'create_product_taxonomies');
function create_product_taxonomies(){
register_taxonomy('brand', 'product', array(
'hierarchical' => true,
'labels' => array(
'name' => __('Brand'),
'singular_name' => __('Brand'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Brand'),
'new_item' => __('New Brand'),
'search_items' => __('Search Brands'),
),
));
register_taxonomy('size', 'product', array(
'hierarchical' => false,
'labels' => array(
'name' => __('Size'),
'singular_name' => __('Size'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Size'),
'new_item' => __('New Size'),
'search_items' => __('Search Sizes'),
),
));
register_taxonomy('price', 'product', array(
'hierarchical' => false,
'labels' => array(
'name' => __('Price'),
'singular_name' => __('Price'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New Price'),
'new_item' => __('New Price'),
'search_items' => __('Search Prices'),
),
));
}
register_taxonomy: Registration 1 custom taxonomy named brand, and the post type is hierarchical product and
Similarly register to add two taxonomy size and brand. Basically with the above 2 code we have a custom post type as you like. Try to create a product, query_posts (), shown on the page look like?
3. Show posts
With the default post, loaded WP will use the template file is single.php to display content. But with custom post type, you will have to declare a single-format file-name custom-post-pype.php (eg: single-product.php with a product name custom post type). In this file you can spend within the normal loop, who used WP also memorized
if (have_posts()){
while (have_posts()){
the_post();
//xuất nội dung của post ở đây.
}
}else{
echo "No posts found";
}
4.Presented list archive.php
WP current version is 3.0.3, with this version of WP does not support archive for custom post type. According to the introduction of a number of developers are doing core of WP, the new 3.1 version that supports this function. But now there are 2 plugins to support our work. One of them that I find it easier to spend a Simple Custom Post Type Archives, after activating this plugin, you will have to create the file types: type-name-custom-post-type.php (eg: type-product.php) and saved in the theme.
File content I can use a loop to display a list of all the normal post as file archive.php
5. query_posts with custom post type
The following code will execute the query post of post type product, with title or content contains letters izwebz, and the post appears as 10
//function dùng để filter
function filter_where($where = '') {
//post_title, post_content like '%izwebz%'
$text = 'izwebz';
$where .= " AND (post_title like '%$text%' OR post_content like '%$text%')";
return $where;
}
// đăng ký filter với WP
add_filter('posts_where', 'filter_where');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
$args = array('post_type' => 'product', 'paged' => $paged,'showposts' => 10 );
query_posts($args);
if(have_posts()){
while(have_posts()){
the_post();
//xuất nội dung ở đây
}
}else{
echo "No products found";
}
//nhớ reset query sau khi xai query_posts()
wp_reset_query();
add_filter: Registering filters with WP. This is one super cool hook technique of WP. If after this post no one has introduced a hook, I would like to volunteer to introduce them in the next post
Cuscom post type to do much directs legal gaiir in the wordpress code, if you use master custom post type it will help you to solve a very large job, help you manage your site better.
WordPress support is very good, so now a lot of websites in the world use wordpress, it can do all the simple to advanced features, I see wordpress can do everything, the web platform information to sales, real estate, airline tickets, hotel, functions nhuu booking.
WordPress is just simple PHP code only, it gives us the pattern is available, we just use them. I wish you success.