我正在wordpress构建中使用自定义帖子类型、元框和;分类法。有一种更简单的方法可以做到这一点。。我还在学习,这是我第一次尝试。不管怎样,我都快完成了,但我想添加一些功能来帮助我的客户机(使其更易于使用)。
我有一个自定义的元框是“日期”,原因是播客的上载日期(即\\u日期)通常与播客录制的日期(元框\\u日期)不同。
我对php中的strotime函数略知一二,我很想用它来处理新播客上“date”字段的数据输入。现在它只是文本,所以如果用户输入“2012年1月23日”或“2012年1月23日”或“1-23-12”等,我就只能使用该文本进行显示。然而,使用strotime函数,我想我可以将用户输入转换为一个日期戳,我可以在网站上随意显示(m、d、y)。
我只是不知道从哪里开始——我对php非常陌生。我将在下面包含相关代码。如果您能提供任何帮助,我们将不胜感激!
谢谢
提姆
// Podcast Meta Box
function podcast_info() {
global $post;
// Noncename needed to verify where the data originated
echo \'<input id="podcastmeta_noncename" name="podcastmeta_noncename" type="hidden" value="\' . wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
// Get the data if its already been entered
$week = get_post_meta($post->ID, \'_week\', true);
$date = get_post_meta($post->ID, \'_date\', true);
$description = get_post_meta($post->ID, \'_description\', true);
// Echo out the field
echo \'<strong>Week</strong><br /><em>What week of the series is this podcast? (enter the ‘Week X’ OR ‘Guest Speaker’)</em>\';
echo \'<input class="widefat" name="_week" type="text" value="\' . $week . \'" />\';
echo \'<strong>Date</strong><br /><em>Enter the Date the podcast was recorded (Format as: ‘mm/dd/yy’)</em>\';
echo \'<input class="widefat" name="_date" type="text" value="\' . $date . \'" />\';
echo \'<strong>Description</strong><br /><em>Enter the text you\\\'d like to display as a description on the media archives list.</em>\';
echo \'<input class="widefat" name="_description" type="text" value="\' . $description . \'" />\';
}
// Save the Podcast Meta box Data
function save_podcast_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'podcastmeta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
// OK, we\'re authenticated: we need to find and save the data
// We\'ll put it into an array to make it easier to loop though.
$podcast_meta[\'_week\'] = $_POST[\'_week\'];
$podcast_meta[\'_date\'] = $_POST[\'_date\'];
$podcast_meta[\'_thumbnail\'] = $_POST[\'_thumbnail\'];
$podcast_meta[\'_seriesimg\'] = $_POST[\'_seriesimg\'];
$podcast_meta[\'_description\'] = $_POST[\'_description\'];
// Convert _date to actual date stamp
foreach ($podcast_meta as $key => $value) {
if(get_post_meta($post->ID, \'_date\', true))
strtotime(\'_date\');
}
// Add values of $podcast_meta as custom fields
foreach ($podcast_meta as $key => $value) { // Cycle through the $podcast_meta array!
if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
$value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn\'t have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action(\'save_post\', \'save_podcast_meta\', 1, 2); // save the custom fields
EDIT - Working Now - See Code Below
<!-- Convert Date to Date Stamp -->
<?php $podcast_date = get_post_meta($post->ID, \'_date\', true);?>
<?php $fixed_date = strtotime($podcast_date); ?>
<!-- Display Date -->
<?php echo date(\'m/d/y\' , $fixed_date) ; ?>