我是Wordpress / PHP开发的新手,在尝试为自定义帖子类型创建一个datepicker字段时遇到了一些问题。当有人试图发布新帖子时,我按照教程添加了一个jquery UI日期选择器,但是该日期选择器没有显示在该页面上。当我点击“发布”时,我会进入一个页面,我可以手动输入日期,但不显示任何日期选择器元数据。任何和所有建议将不胜感激!!!
这是我的自定义帖子类型代码(道歉的长度!):
<?php
/**
* Plugin Name: rundown
* Plugin URI: #
* Version: 1.0
* Author: bjorkland
* Author URI: http://localhost:8888/wp-admin/plugins.php? plugin_status=all&paged=1&s
* Description: A custom post type
* License: GPL2
*/
class Rundown{
function __construct() {
add_action( 'init', array( $this, 'register_custom_post_type' ) );
}
function register_custom_post_type() {
register_post_type( 'rundown', array(
'labels' => array(
'name' => _x( 'Rundowns', 'post type general name', 'rundown' ),
'singular_name' => _x( 'Rundown', 'post type singular name', 'rundown' ),
'menu_name' => _x( 'Rundown', 'admin menu', 'rundown' ),
'name_admin_bar' => _x( 'Rundown', 'add new on admin bar', 'rundown' ),
'add_new' => _x( 'Add New Rundown', 'rundown' ),
'add_new_item' => __( 'Add New Rundown', 'rundown' ),
'new_item' => __( 'New Rundown', 'rundown' ),
'edit_item' => __( 'Edit Rundown', 'rundown' ),
'view_item' => __( 'View Rundown', 'rundown' ),
'all_items' => __( 'All Rundowns', 'rundown' ),
'search_items' => __( 'Search Rundowns', 'rundown' ),
'parent_item_colon' => __( 'Parent Rundown:', 'rundown' ),
'not_found' => __( 'No Rundown found.', 'rundown' ),
'not_found_in_trash' => __( 'No Rundown found in Trash.', 'rundown' ),
),
// Frontend
'has_archive' => false,
'public' => false,
'publicly_queryable' => false,
// Admin
'capability_type' => 'post',
'menu_icon' => 'dashicons-businessman',
'menu_position' => 10,
'query_var' => true,
'show_in_menu' => true,
'show_ui' => true,
'supports' => array(
'title',
'author',
'comments',
),
) );
}
}
$Rundown = new Rundown;
function rundown_add_info_meta_box() {
add_meta_box( 'rundown_meta_box',
__( 'Event Info', 'rundown' ),
'rundown_render_event_info_metabox',
'event',
'side',
'core'
);
}
add_action( 'add_meta_boxes', 'rundown_add_info_metabox' );
function rundown_render_event_info_metabox( $post ) {
wp_nonce_field( basename( __FILE__ ), 'rundown-event-info-nonce' );
$event_start_date = get_post_meta( $post->ID, 'event-start-date', true );
$event_end_date = get_post_meta( $post->ID, 'event-end-date', true );
$event_start_date = ! empty( $event_start_date ) ? $event_start_date : time();
$event_end_date = ! empty( $event_end_date ) ? $event_end_date : $event_start_date;
}
function rundown_admin_script_style( $hook ) {
global $post_type;
if ( 'post.php' == $hook || 'post-new.php' == $hook ) {
wp_enqueue_script(
'upcoming-events',
SCRIPTS . 'script.js',
array( 'jquery', 'jquery-ui-datepicker' ),
'1.0',
true
);
wp_enqueue_style(
'jquery-ui-calendar',
STYLES . 'jquery-ui-1.10.4.custom.min.css',
false,
'1.10.4',
'all'
);
}
}
add_action( 'admin_enqueue_scripts', 'rundown_admin_script_style' );
?>
<label for="rundown-event-start-date"><?php _e( 'Event Start Date:', 'rundown' ); ?></label>
<input class="widefat rundown-event-date-input" id="rundown-event-start-date" type="text" name="rundown-event-start-date" placeholder="Format: February 18, 2014" value="<?php echo date( 'F d, Y', $event_start_date ); ?> " />
<label for="rundown-event-end-date"><?php _e( 'Event End Date:', 'rundown' ); ?></label>
<input class="widefat rundown-event-date-input" id="rundown-event-end-date" type="text" name="rundown-event-end-date" placeholder="Format: February 18, 2014" value="<?php echo date( 'F d, Y', $event_end_date ); ?>" />
这是我的jQuery代码,在我的wp-includes / js文件夹的script.js文件中:
(function( $ ) {
$( '#rundown-event-start-date' ).datepicker({
dateFormat: 'MM dd, yy',
onClose: function( selectedDate ){
$( '#rundown-event-end-date' ).datepicker( 'option', 'minDate', selectedDate );
}
});
$( '#rundown-event-end-date' ).datepicker({
dateFormat: 'MM dd, yy',
onClose: function( selectedDate ){
$( '#rundown-event-start-date' ).datepicker( 'option', 'maxDate', selectedDate );
}
});
})( jQuery );