注册了一个自定义块类型的文章,支持分类,并在后台文章列表中显示shortcode,目的是为了方便调用。代码如下:
<?php/*Plugin Name: 块简码插件Description: 注册一个自定义块类型的文章,支持分类,并在后台文章列表中显示shortcode。Version: 1.0Text Domain: block_shortcodeAuthor: 殷江碧*/// 注册块类型文章function register_block_shortcode_post_type() {$labels = array('name' => _x('块简码', '文章类型通用名称', 'your-text-domain'),'singular_name' => _x('块简码', '文章类型单数名称', 'your-text-domain'),'menu_name' => _x('块简码', '后台菜单', 'your-text-domain'),'name_admin_bar' => _x('块简码', '在管理工具栏上添加新项目', 'your-text-domain'),'add_new' => _x('添加新块简码', '文章', 'your-text-domain'),'add_new_item' => __('添加新块简码', 'your-text-domain'),'new_item' => __('新块简码', 'your-text-domain'),'edit_item' => __('编辑块简码', 'your-text-domain'),'view_item' => __('查看块简码', 'your-text-domain'),'all_items' => __('所有块简码', 'your-text-domain'),'search_items' => __('搜索块简码', 'your-text-domain'),'parent_item_colon' => __('父级块简码:', 'your-text-domain'),'not_found' => __('未找到块简码。', 'your-text-domain'),'not_found_in_trash' => __('回收站中未找到块简码。', 'your-text-domain'));$args = array('labels' => $labels,'public' => true,'publicly_queryable' => true,'show_ui' => true,'show_in_menu' => true,'query_var' => true,'rewrite' => array('slug' => 'block-shortcode'),'capability_type' => 'post','has_archive' => true,'hierarchical' => false,'menu_position' => null,'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),'taxonomies' => array('block-shortcode-category'),'show_in_rest' => true, // 添加 show_in_rest 参数);register_post_type('block-shortcode', $args);}add_action('init', 'register_block_shortcode_post_type');// 注册块简码分类function register_block_shortcode_category_taxonomy() {$labels = array('name' => _x('块简码分类', '分类法通用名称', 'your-text-domain'),'singular_name' => _x('块简码分类', '分类法单数名称', 'your-text-domain'),'search_items' => __('搜索块简码分类', 'your-text-domain'),'all_items' => __('所有块简码分类', 'your-text-domain'),'parent_item' => __('父级块简码分类', 'your-text-domain'),'parent_item_colon' => __('父级块简码分类:', 'your-text-domain'),'edit_item' => __('编辑块简码分类', 'your-text-domain'),'update_item' => __('更新块简码分类', 'your-text-domain'),'add_new_item' => __('添加新块简码分类', 'your-text-domain'),'new_item_name' => __('新块简码分类名称', 'your-text-domain'),'menu_name' => __('块简码分类', 'your-text-domain'),);$args = array('hierarchical' => true,'labels' => $labels,'show_ui' => true,'show_admin_column' => true,'query_var' => true,'rewrite' => array('slug' => 'block-shortcode-category'),'show_in_rest' => true, // 添加 show_in_rest 参数);register_taxonomy('block-shortcode-category', array('block-shortcode'), $args);}add_action('init', 'register_block_shortcode_category_taxonomy');// 添加块简码短代码function block_shortcode_callback($atts) {$atts = shortcode_atts(array('id' => '',),$atts,'block_shortcode');$post_id = isset($atts['id']) ? intval($atts['id']) : 0;$output = '';if ($post_id > 0) {$output = get_post_field('post_content', $post_id);}return $output;}add_shortcode('block_shortcode', 'block_shortcode_callback');// 在后台列中显示短代码function display_shortcode_in_admin_column($column, $post_id) {if ($column == 'shortcode') {$shortcode = '[block_shortcode id=' . $post_id . ']';echo esc_html($shortcode);}}add_action('manage_block-shortcode_posts_custom_column', 'display_shortcode_in_admin_column', 10, 2);// 添加短代码列到后台function add_shortcode_column_to_admin($columns) {$columns['shortcode'] = __('短代码', 'your-text-domain');return $columns;}add_filter('manage_block-shortcode_posts_columns', 'add_shortcode_column_to_admin');<?php /* Plugin Name: 块简码插件 Description: 注册一个自定义块类型的文章,支持分类,并在后台文章列表中显示shortcode。 Version: 1.0 Text Domain: block_shortcode Author: 殷江碧 */ // 注册块类型文章 function register_block_shortcode_post_type() { $labels = array( 'name' => _x('块简码', '文章类型通用名称', 'your-text-domain'), 'singular_name' => _x('块简码', '文章类型单数名称', 'your-text-domain'), 'menu_name' => _x('块简码', '后台菜单', 'your-text-domain'), 'name_admin_bar' => _x('块简码', '在管理工具栏上添加新项目', 'your-text-domain'), 'add_new' => _x('添加新块简码', '文章', 'your-text-domain'), 'add_new_item' => __('添加新块简码', 'your-text-domain'), 'new_item' => __('新块简码', 'your-text-domain'), 'edit_item' => __('编辑块简码', 'your-text-domain'), 'view_item' => __('查看块简码', 'your-text-domain'), 'all_items' => __('所有块简码', 'your-text-domain'), 'search_items' => __('搜索块简码', 'your-text-domain'), 'parent_item_colon' => __('父级块简码:', 'your-text-domain'), 'not_found' => __('未找到块简码。', 'your-text-domain'), 'not_found_in_trash' => __('回收站中未找到块简码。', 'your-text-domain') ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'block-shortcode'), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'), 'taxonomies' => array('block-shortcode-category'), 'show_in_rest' => true, // 添加 show_in_rest 参数 ); register_post_type('block-shortcode', $args); } add_action('init', 'register_block_shortcode_post_type'); // 注册块简码分类 function register_block_shortcode_category_taxonomy() { $labels = array( 'name' => _x('块简码分类', '分类法通用名称', 'your-text-domain'), 'singular_name' => _x('块简码分类', '分类法单数名称', 'your-text-domain'), 'search_items' => __('搜索块简码分类', 'your-text-domain'), 'all_items' => __('所有块简码分类', 'your-text-domain'), 'parent_item' => __('父级块简码分类', 'your-text-domain'), 'parent_item_colon' => __('父级块简码分类:', 'your-text-domain'), 'edit_item' => __('编辑块简码分类', 'your-text-domain'), 'update_item' => __('更新块简码分类', 'your-text-domain'), 'add_new_item' => __('添加新块简码分类', 'your-text-domain'), 'new_item_name' => __('新块简码分类名称', 'your-text-domain'), 'menu_name' => __('块简码分类', 'your-text-domain'), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'block-shortcode-category'), 'show_in_rest' => true, // 添加 show_in_rest 参数 ); register_taxonomy('block-shortcode-category', array('block-shortcode'), $args); } add_action('init', 'register_block_shortcode_category_taxonomy'); // 添加块简码短代码 function block_shortcode_callback($atts) { $atts = shortcode_atts( array( 'id' => '', ), $atts, 'block_shortcode' ); $post_id = isset($atts['id']) ? intval($atts['id']) : 0; $output = ''; if ($post_id > 0) { $output = get_post_field('post_content', $post_id); } return $output; } add_shortcode('block_shortcode', 'block_shortcode_callback'); // 在后台列中显示短代码 function display_shortcode_in_admin_column($column, $post_id) { if ($column == 'shortcode') { $shortcode = '[block_shortcode id=' . $post_id . ']'; echo esc_html($shortcode); } } add_action('manage_block-shortcode_posts_custom_column', 'display_shortcode_in_admin_column', 10, 2); // 添加短代码列到后台 function add_shortcode_column_to_admin($columns) { $columns['shortcode'] = __('短代码', 'your-text-domain'); return $columns; } add_filter('manage_block-shortcode_posts_columns', 'add_shortcode_column_to_admin');<?php /* Plugin Name: 块简码插件 Description: 注册一个自定义块类型的文章,支持分类,并在后台文章列表中显示shortcode。 Version: 1.0 Text Domain: block_shortcode Author: 殷江碧 */ // 注册块类型文章 function register_block_shortcode_post_type() { $labels = array( 'name' => _x('块简码', '文章类型通用名称', 'your-text-domain'), 'singular_name' => _x('块简码', '文章类型单数名称', 'your-text-domain'), 'menu_name' => _x('块简码', '后台菜单', 'your-text-domain'), 'name_admin_bar' => _x('块简码', '在管理工具栏上添加新项目', 'your-text-domain'), 'add_new' => _x('添加新块简码', '文章', 'your-text-domain'), 'add_new_item' => __('添加新块简码', 'your-text-domain'), 'new_item' => __('新块简码', 'your-text-domain'), 'edit_item' => __('编辑块简码', 'your-text-domain'), 'view_item' => __('查看块简码', 'your-text-domain'), 'all_items' => __('所有块简码', 'your-text-domain'), 'search_items' => __('搜索块简码', 'your-text-domain'), 'parent_item_colon' => __('父级块简码:', 'your-text-domain'), 'not_found' => __('未找到块简码。', 'your-text-domain'), 'not_found_in_trash' => __('回收站中未找到块简码。', 'your-text-domain') ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'block-shortcode'), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'), 'taxonomies' => array('block-shortcode-category'), 'show_in_rest' => true, // 添加 show_in_rest 参数 ); register_post_type('block-shortcode', $args); } add_action('init', 'register_block_shortcode_post_type'); // 注册块简码分类 function register_block_shortcode_category_taxonomy() { $labels = array( 'name' => _x('块简码分类', '分类法通用名称', 'your-text-domain'), 'singular_name' => _x('块简码分类', '分类法单数名称', 'your-text-domain'), 'search_items' => __('搜索块简码分类', 'your-text-domain'), 'all_items' => __('所有块简码分类', 'your-text-domain'), 'parent_item' => __('父级块简码分类', 'your-text-domain'), 'parent_item_colon' => __('父级块简码分类:', 'your-text-domain'), 'edit_item' => __('编辑块简码分类', 'your-text-domain'), 'update_item' => __('更新块简码分类', 'your-text-domain'), 'add_new_item' => __('添加新块简码分类', 'your-text-domain'), 'new_item_name' => __('新块简码分类名称', 'your-text-domain'), 'menu_name' => __('块简码分类', 'your-text-domain'), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'block-shortcode-category'), 'show_in_rest' => true, // 添加 show_in_rest 参数 ); register_taxonomy('block-shortcode-category', array('block-shortcode'), $args); } add_action('init', 'register_block_shortcode_category_taxonomy'); // 添加块简码短代码 function block_shortcode_callback($atts) { $atts = shortcode_atts( array( 'id' => '', ), $atts, 'block_shortcode' ); $post_id = isset($atts['id']) ? intval($atts['id']) : 0; $output = ''; if ($post_id > 0) { $output = get_post_field('post_content', $post_id); } return $output; } add_shortcode('block_shortcode', 'block_shortcode_callback'); // 在后台列中显示短代码 function display_shortcode_in_admin_column($column, $post_id) { if ($column == 'shortcode') { $shortcode = '[block_shortcode id=' . $post_id . ']'; echo esc_html($shortcode); } } add_action('manage_block-shortcode_posts_custom_column', 'display_shortcode_in_admin_column', 10, 2); // 添加短代码列到后台 function add_shortcode_column_to_admin($columns) { $columns['shortcode'] = __('短代码', 'your-text-domain'); return $columns; } add_filter('manage_block-shortcode_posts_columns', 'add_shortcode_column_to_admin');
© 版权声明
THE END
暂无评论内容