前端怎么做自己研究吧,我自己是上网扒了个并且整了个三级联动的,高德地图api需要看文档,按文档自己申请填上,一共需要3个
<?php
/* 增加门店发布类型 */
//增加门店类型文章
add_action( 'init', 'create_store_type' );
function create_store_type() {
$labels = array(
'name' => _x( '门店', 'post type general name', 'textdomain' ),
'singular_name' => _x( '门店', 'post type singular name', 'textdomain' ),
'menu_name' => _x( '门店', 'admin menu', 'textdomain' ),
'name_admin_bar' => _x( '门店', 'add new on admin bar', 'textdomain' ),
'add_new' => _x( '添加门店', 'store', 'textdomain' ),
'add_new_item' => __( '添加门店', 'textdomain' ),
'new_item' => __( '新门店', 'textdomain' ),
'edit_item' => __( '编辑门店', 'textdomain' ),
'view_item' => __( '查看门店', 'textdomain' ),
'all_items' => __( '所有门店', 'textdomain' ),
'search_items' => __( '搜索门店', 'textdomain' ),
'parent_item_colon' => __( '父级门店:', 'textdomain' ),
'not_found' => __( '未找到门店', 'textdomain' ),
'not_found_in_trash' => __( '回收站中未找到门店', 'textdomain' ),
'featured_image' => _x( '门店封面图', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
'set_featured_image' => _x( '设置门店封面图', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'remove_featured_image' => _x( '移除门店封面图', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'use_featured_image' => _x( '使用门店封面图', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'archives' => _x( '门店存档', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
'insert_into_item' => _x( '插入门店', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
'uploaded_to_this_item' => _x( '上传到此门店', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
'filter_items_list' => _x( '筛选门店列表', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
'items_list_navigation' => _x( '门店列表导航', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
'items_list' => _x( '门店列表', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
);
$args = array(
'label' => __( '门店', 'textdomain' ),
'description' => __( '这是一个门店类型', 'textdomain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions' ), // 添加 'revisions' 支持
'taxonomies' => array( 'store_type', 'post_tag' ), // 添加支持的分类法
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_in_rest' => true,
'rest_base' => 'store',
'rest_controller_class' => 'WP_REST_Posts_Controller', // 指定 REST API 控制器类名
);
register_post_type( 'store', $args );
}
// // 门店自定义分类,如果需要可开启,开启后需要增加single-archive-store.php
// function store_taxonomies() {
// register_taxonomy(
// 'store_category', //自定义分类名
// 'store', //自定义类型
// array(
// 'labels' => array(
// 'name' => '门店分类',
// 'add_new_item' => '添加门店分类',
// 'new_item_name' => '新门店分类',
// ),
// 'show_ui' => true, //后台显示
// 'show_tagcloud' => false, //是否显示标签云
// 'hierarchical' => true, //控制分类法格式
// )
// );
// }
// add_action( 'init', 'store_taxonomies', 0 );
// 指定门店页面模板,子主题根目录下single-store的模板;
// 列表页模板为single-archive,暂时没有,如有需要可增加
function store_template( $template_path ) {
if ( get_post_type() == 'store' ) {
if ( is_single() ) {
$theme_file = locate_template( array ( 'single-store.php' ) );
$template_path = $theme_file;
} elseif ( is_archive() ) {
$theme_file = locate_template( array ( 'page-stores.php' ) );
$template_path = $theme_file;
}
}
return $template_path;
}
add_filter( 'template_include', 'store_template', 1 );
function mymetabox_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
//门店自定义字段metabox
/**
* Generated by the WordPress Meta Box Generator
* https://jeremyhixon.com/tool/wordpress-meta-box-generator/
*
* Retrieving the values:
* Label1 = get_post_meta( get_the_ID(), 'Keylabel1', true )
* Label2 = get_post_meta( get_the_ID(), 'Keylabel2', true )
* Label3 = get_post_meta( get_the_ID(), 'Keylabel3', true )
*/
//门店自定义字段metabox
/**
* Generated by the WordPress Meta Box Generator
* https://jeremyhixon.com/tool/wordpress-meta-box-generator/
*
* Retrieving the values:
* Label1 = get_post_meta( get_the_ID(), 'Keylabel1', true )
* Label2 = get_post_meta( get_the_ID(), 'Keylabel2', true )
* Label3 = get_post_meta( get_the_ID(), 'Keylabel3', true )
*/
class storeMetabox {
private $config = '{"title":"门店信息","description":"","prefix":"Key","domain":"Text","class_name":"Text","post-type":["store"],"context":"normal","priority":"high","cpt":"Custom","fields":[{"type":"text","label":"门店地址(必填)","default":"请输入门店地址","id":"store_address", "required": true},{"type":"text","label":"门店经纬度(必填)","default":"请输入门店经纬度","id":"store_coordinate", "required": true},{"type":"text","label":"门店电话","default":"123123123","id":"store_phone", "required":false}]}';
public function __construct() {
$this->config = json_decode( $this->config, true );
$this->process_cpts();
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'admin_head', [ $this, 'admin_head' ] );
add_action( 'save_post', [ $this, 'save_post' ] );
add_action( 'rest_api_init', [ $this, 'register_rest_field' ] ); // 注册 REST API 的字段
}
public function process_cpts() {
if ( !empty( $this->config['cpt'] ) ) {
if ( empty( $this->config['post-type'] ) ) {
$this->config['post-type'] = [];
}
$parts = explode( ',', $this->config['cpt'] );
$parts = array_map( 'trim', $parts );
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts );
}
}
public function add_meta_boxes() {
foreach ( $this->config['post-type'] as $screen ) {
add_meta_box(
sanitize_title( $this->config['title'] ),
$this->config['title'],
[ $this, 'add_meta_box_callback' ],
$screen,
$this->config['context'],
$this->config['priority']
);
}
}
public function admin_head() {
global $typenow;
if ( in_array( $typenow, $this->config['post-type'] ) ) {
?>config['fields'] as $field ) {
switch ( $field['type'] ) {
default:
if ( isset( $_POST[ $field['id'] ] ) ) {
$sanitized = sanitize_text_field( $_POST[ $field['id'] ] );
update_post_meta( $post_id, $field['id'], $sanitized );
}
}
}
}
public function add_meta_box_callback() {
$tips='点击使用高德地图获取经纬度
zke:需要填写完整地址,包含【省/自治区、市/区】,便于前端地图正确显示,如:北京市海淀区xxxxxxxxx';
echo '' . $this->config['description'] . '';
echo ''.$tips.'';
$this->fields_table();
}
private function fields_table() {
?>| label( $field ); ?> | field( $field ); ?> |
|---|
本文作者:ZKCOI
文章名称:WP基于高德地图的门店功能,增加获取门店json函数,支持短代码
文章链接:https://www.zkcoi.com/365up/program/2342.html
本站资源仅供个人学习和交流,如若转载,请注明出处,详见《免责声明》。
微信扫一扫
支付宝扫一扫 