-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.php
84 lines (66 loc) · 2.02 KB
/
books.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
Plugin Name:Books
Description:Testing
Author:Percy Michael
*/
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'menu_name' => __( 'Books' ),
'singular_name' => __( 'Book' ),
'all_items' => __( 'All Books' ),
'name' => __( 'Books' ),
'add_new_item' => __( 'Add New Book'),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit This Book' ),
'search_items' => __( 'Search Books' )
),
'public' => true,
'menu_position' => 2,
'register_meta_box_cb'=>'meta_box',
'supports' => array( 'title','custom-fields',)
)
);
}
add_action( 'init', 'create_post_type' );
function meta_box(){
add_meta_box('meta_box_customfield','Custom meta Field','meta_box_display','acme_product','normal','high');
}
add_action('add_meta_boxes','meta_box');
function meta_box_display(){
global $post;
$book_tag=get_post_meta($post->ID,'book_tag',true);
?>
<label>book_tag</label>
<input type="text" name="book_tag" class="widefat" value="<?php print($book_tag); ?>">
<?php
}
function save_meta_box($book_id)
{
$is_autosave=wp_is_post_autosave($book_id);
$is_revision=wp_is_post_revision($book_id);
if ($is_autosave || $is_revision) {
return;
}
$book=get_post($book_id);
if($book->post_type=='acme_product'){
/*save data*/
if (array_key_exists('book_tag',$_POST)) {
update_post_meta($book_id,'book_tag',$_POST['book_tag']);
}
}
}
add_action('save_post','save_meta_box');
//display posts
function get_books()
{
$args=array('posts_per_page'=>100,'post_type'=>'acme_product');
$books=get_posts($args);
foreach ($books as $key => $value) {
print($value->post_title.'<br>');
}
}
add_shortcode('book_shortcode','get_books');
?>