WordPress添加公告模块功能,非插件模式,本文章已日主题为例,加入到日主题的 网站动态 数据列表中;

第一步:在主题funtions.php同目录下,创建notice.php文件,并将以下代码复制到notice.php文件中

<?php
/**
* Created by PhpStorm.
* User: YMADAO.COM
* Date: 2022/7/28
* Time: 18:08
*/
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 ,
‘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 );

第二步:在functions.php中引用notice.php文件,将如下代码复制到functions.php底部:

include (“notice.php”);

添加完以上内容后,重新登录一次WordPress后台,进入后台后,你会发现在左边菜单栏中,出现了公告菜单项:

WordPress后台 公告菜单栏截图

第三步:在日主题的 网站动态 模块中,添加公告的输出:

如果有宝塔,用宝塔打开wp-content/themes/ripro-v2/inc/options/widget-options.php文件,滚动到157行,if (!function_exists(‘rizhuti_v2_module_dynamic’)) 代码处。

替换整个if判断,看不懂代码者,直接将以下代码直接替换整个if(){}块:

<?php
/**
* Created by PhpStorm.
* User: YMADAO.COM
* Date: 2022/7/28
* Time: 18:08
*/
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 ,
‘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 );

 

基于以上操作,基本已完成配置,可以去WordPress后台的公告菜单处,发布公告:

发布后,效果如下:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注