Reputation: 549
I creating a theme for WordPress, I have tried this tutorial on creating WordPress custom post, and also did some research but struggling to find a clear answer(on custom post)
I am trying something like this:
+----------------------------+
|Post-type: Books |
+----------------------------+
|Name: Les misérables |
|Author: Victor Hugo |
|Genre: Poesy |
|Year-of-Pub: 1862 |
+----------------------------+
How can I create a custom post that allow me to add, remove and edit a book and all the details related to it. And display it as any post.
Upvotes: 0
Views: 186
Reputation: 610
create a new post type in Wordpress is pretty easy, you just need to edit the modify the right files, what I normally do is create a new plugin (which is simply a new folder in the wordpress folder system), create a php file inside your plugin folder with the same plugin name.
here is the code to create an empty post type, that in your case it's gonna be "book"
<?php
function dwwp_register_post_type() {
$args = array('public'=> true, 'label'=> 'Staff');
register_post_type( 'staff', $args);
}
add_action( 'init', 'dwwp_register_post_type' );
if you want to specify more information for your custom post type:
<?php
//Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function dwwp_register_post_type() {
$singular = 'Job';
$plural = 'Jobs';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $singular,
'edit' => 'Edit',
'edit_item' => 'Edit ' . $singular,
'new_item' => 'New ' . $singular,
'view' => 'View ' . $singular,
'view_item' => 'View ' . $singular,
'search_term' => 'Search ' . $plural,
'parent' => 'Parent ' . $singular,
'not_found' => 'No ' . $plural .' found',
'not_found_in_trash' => 'No ' . $plural .' in Trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-businessman',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
// 'capabilities' => array(),
'rewrite' => array(
'slug' => $slug,
'with_front' => true,
'pages' => true,
'feeds' => true,
),
'supports' => array(
'title',
'editor',
'author',
'custom-fields'
)
);
register_post_type( $slug, $args );
}
add_action( 'init', 'dwwp_register_post_type' );
Also have a look to this tutorial if you struggle... http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress
good luck.
Upvotes: 1