本文主要参考初缘小站和文字咖两位站长的文章,对其中失效的地方进行了修改。在此对二位表示感谢!
先看下效果:
1 后台添加公告按钮
首先需要在后台添加公告按钮,如图:
在functions.php同级目录下新建mcj_gonggao.php文件,代码如下:
function post_type_bulletin() {
register_post_type(
'bulletin',
array( 'public' => true,
'publicly_queryable' => true,
'hierarchical' => false,
'labels'=>array(
'name' => _x('公告', 'post type general name'),
'singular_name' => _x('公告', 'post type singular name'),
'add_new' => _x('添加新公告', '公告'),
'add_new_item' => __('添加新公告'),
'edit_item' => __('编辑公告'),
'new_item' => __('新的公告'),
'view_item' => __('预览公告'),
'search_items' => __('搜索公告'),
'not_found' => __('您还没有发布公告'),
'not_found_in_trash' => __('回收站中没有公告'),
'parent_item_colon' => ''
),
'show_ui' => true,
'menu_position'=>5,
'supports' => array(
'title',
'author',
'excerpt',
'thumbnail',
'trackbacks',
'editor',
'comments',
'custom-fields',
'revisions' ) ,
'show_in_nav_menus' => true ,
'menu_icon' => 'dashicons-megaphone',
'taxonomies' => array(
'menutype',
'post_tag')
)
);
}
add_action('init', 'post_type_bulletin');
function create_genre_taxonomy() {
$labels = array(
'name' => _x( '公告分类', 'taxonomy general name' ),
'singular_name' => _x( 'genre', 'taxonomy singular name' ),
'search_items' => __( '搜索分类' ),
'all_items' => __( '全部分类' ),
'parent_item' => __( '父级分类目录' ),
'parent_item_colon' => __( '父级分类目录:' ),
'edit_item' => __( '编辑公告分类' ),
'update_item' => __( '更新' ),
'add_new_item' => __( '添加新公告分类' ),
'new_item_name' => __( 'New Genre Name' ),
);
register_taxonomy('genre',array('bulletin'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
));
}
add_action( 'init', 'create_genre_taxonomy', 0 );
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
functionpost_type_bulletin(){
register_post_type(
'bulletin',
array('public'=>true,
'publicly_queryable'=>true,
'hierarchical'=>false,
'labels'=>array(
'name'=>_x('公告','post type general name'),
'singular_name'=>_x('公告','post type singular name'),
'add_new'=>_x('添加新公告','公告'),
'add_new_item'=>__('添加新公告'),
'edit_item'=>__('编辑公告'),
'new_item'=>__('新的公告'),
'view_item'=>__('预览公告'),
'search_items'=>__('搜索公告'),
'not_found'=>__('您还没有发布公告'),
'not_found_in_trash'=>__('回收站中没有公告'),
'parent_item_colon'=>''
),
'show_ui'=>true,
'menu_position'=>5,
'supports'=>array(
'title',
'author',
'excerpt',
'thumbnail',
'trackbacks',
'editor',
'comments',
'custom-fields',
'revisions'),
'show_in_nav_menus'=>true,
'menu_icon'=>'dashicons-megaphone',
'taxonomies'=>array(
'menutype',
'post_tag')
)
);
}
add_action('init','post_type_bulletin');
functioncreate_genre_taxonomy(){
$labels=array(
'name'=>_x('公告分类','taxonomy general name'),
'singular_name'=>_x('genre','taxonomy singular name'),
'search_items'=>__('搜索分类'),
'all_items'=>__('全部分类'),
'parent_item'=>__('父级分类目录'),
'parent_item_colon'=>__('父级分类目录:'),
'edit_item'=>__('编辑公告分类'),
'update_item'=>__('更新'),
'add_new_item'=>__('添加新公告分类'),
'new_item_name'=>__('New Genre Name'),
);
register_taxonomy('genre',array('bulletin'),array(
'hierarchical'=>true,
'labels'=>$labels,
'show_ui'=>true,
'query_var'=>true,
'rewrite'=>array('slug'=>'genre'),
));
}
add_action('init','create_genre_taxonomy',0);
然后,打开functions.php文件,引用刚刚建立的文件:
include ("mcj_gonggao.php");
1
include("mcj_gonggao.php");
这时如果我们登陆后台,就会发现在文章下方多了一个公告按钮。
2 添加位置
把下面代码放到index.php相应位置:
<?php $loop = new WP_Query( array( 'post_type' => 'bulletin', 'posts_per_page' => _hui('bulletin_n') ) );
while ( $loop->have_posts() ) : $loop->the_post();
echo '
'.get_the_title().get_the_subtitle().'';
endwhile; wp_reset_query(); ?>
1
2
3
4
5
6
7
8
9
10
11