Before understanding the advanced part of it let me explain you in brief about WordPress. It is an FOSS CMS i.e. content management system powered by PHP and MySQL. Among many features it provides the important features are plug-in architecture and a template system which makes this CMS a robust one. It is used by over 10% of global websites.
Now moving to one of the most exciting features coming in WordPress 3.0 is custom post types, which will vastly expand WordPress’ CMS capabilities. This feature will come handy especially when using WordPress as a CMS.
WordPress Custom Post types are basically different kinds of building blocks that when put together form our website. To understand it in a better way let us create our custom post type called “place”.
Any WordPress system will have functions.php in themes folder. This is the one of our interest. To achieve the purpose you will have to add the following code at the end of the functions.php file.
function post_type_place() {
register_post_type('place', array('labels' => array('name'=>__('Places'),'add_new_item' => __( 'Add New Place' ),'edit_item' =>__( 'Edit Place' )),
'public' => true,
'show_ui' => true,
'supports' => array('title','author','editor',
'post-thumbnails',
'excerpts',
'trackbacks',
'custom-fields',
'comments',
'revisions'),
'rewrite' => false,
'_builtin' => false,
'_edit_link' => 'post.php?post=%d',
'capability_type' => 'post',
'hierarchical' => false,'query_var' => true
)
);
// add to our plugin init function
//for single template
global $wp_rewrite;
$place_structure = '/place/%category%/%place%';
$wp_rewrite->add_rewrite_tag("%place%", '([^/]+)', "place=");
$wp_rewrite->add_permastruct('place', $place_structure, false);
//for displaying tags
register_taxonomy_for_object_type('post_tag', 'place');
//for displaying categories
register_taxonomy_for_object_type('category', 'place');
}
// Add filter to plugin init function
If you want you can use a single page template for displaying custom post.
//function to redirect to place page(templating system)
// Add filter to plugin init function
add_filter('post_type_link', 'place_permalink', 10, 3);
// Adapted from get_permalink function in wp-includes/link-template.php
function place_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
$leavename? '' : '%postname%',
'%post_id%',
'%category%',
'%author%',
$leavename? '' : '%pagename%',
);
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
$unixtime = strtotime($post->post_date);
$category = '';
if ( strpos($permalink, '%category%') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, '_usort_terms_by_ID'); // order by ID
$category = $cats[0]->slug;
if($category == "featured") {
$category = $cats[1]->slug;}
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, '/', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( 'default_category' ) );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( strpos($permalink, '%author%') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$rewritereplace =
array(
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
} else { // if they're not using the fancy permalink option
}
return $permalink;
}
add_action('init', 'post_type_place');
Update the permalinks.
The newly created post type (place) will look like this in the WordPress administrator dashboard under the links panel:
Now after creating a custom post you will want to use it. Now see the image below to understand how to add new place in WordPress.
Once you add couple of places the list view under WordPress dashboard will look something like below.
To be able to display the single-type-template using WordPress you will have to use a PHP file by the name single.php. To display newly created custom post you will have to use single-{posttype}.php. So in our case the filename would be single-place.php.
No comments:
Post a Comment