将此代码添加到functions.php
文件此代码用于自定义post类型明细表。根据需要更改帖子类型。
// Add to our admin_init function
add_filter(\'manage_schedule_posts_columns\', \'myown_add_post_columns\');
function myown_add_post_columns($columns) {
$columns[\'stime\'] = \'Start Time\';
$columns[\'etime\'] = \'End Time\';
return $columns;
}
// Add to our admin_init function
add_action(\'manage_schedule_posts_custom_column\', \'myown_render_post_columns\', 10, 2);
function myown_render_post_columns($column_name, $id) {
switch ($column_name) {
case \'stime\':
// show my_field
echo get_post_meta( $id, \'stime\', TRUE);
case \'etime\':
// show my_field
$my_fieldvalue1 = get_post_meta( $id, \'etime\', TRUE);
echo $my_fieldvalue1;
}
}
// Add to our admin_init function
add_action(\'quick_edit_custom_box\', \'myown_add_quick_edit\', 10, 2);
function myown_add_quick_edit($column_name, $post_type) {
if ($column_name != \'stime\') return;
?>
<fieldset class="inline-edit-col-left">
<div class="inline-edit-col">
<span class="title">start time</span>
<input id="myfield_noncename" type="hidden" name="myfield_noncename" value="" />
<input id="myfield" type="text" name="stime" value=""/></br>
<span class="title">End time</span>
<input id="myfield1" type="text" name="etime" value=""/></br>
<div id="main">
<span class="title">Add New Session date</span></br>
<a href="#" class="aclick">Add new</a></br></div>
</div>
</fieldset>
<?php
}
// Add to our admin_init function
add_action(\'save_post\', \'myown_save_quick_edit_data\');
function myown_save_quick_edit_data($post_id) {
// verify if this is an auto save routine.
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( \'stime\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
}
if ( \'etime\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
}
else {
if ( !current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
if(isset($_POST[\'tag-name\']))
{
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;
$post = get_post($post_id);
if ( $post->post_type == \'schedule\') { // change \'post\' to any cpt you want to target
$term = get_term_by(\'slug\', $post->post_name, \'schedule_category\');
if ( empty($term) ) {
$add = wp_insert_term( $_POST[\'tag-name\'], \'schedule_category\', array(\'slug\'=> $_POST[\'tag-name\']) );
if ( is_array($add) && isset($add[\'term_id\']) ) {
wp_set_object_terms($post_id, $add[\'term_id\'], \'schedule_category\', true );
}
}
}
}
// Authentication passed now we save the data
if (isset($_POST[\'stime\']) && ($post->post_type != \'revision\')) {
$my_fieldvalue = esc_attr($_POST[\'stime\']);
if ($my_fieldvalue)
update_post_meta( $post_id, \'stime\', $my_fieldvalue);
else
delete_post_meta( $post_id, \'stime\');
}
return $my_fieldvalue;
if (isset($_POST[\'etime\']) && ($post->post_type != \'revision\')) {
$my_fieldvalue1 = esc_attr($_POST[\'etime\']);
if ($my_fieldvalue1)
update_post_meta( $post_id, \'etime\', $my_fieldvalue1);
else
delete_post_meta( $post_id, \'etime\');
}
return $my_fieldvalue1;
}
// Add to our admin_init function
add_action(\'admin_footer\', \'myown_quick_edit_javascript\');
function myown_quick_edit_javascript() {
global $current_screen;
if (($current_screen->post_type != \'schedule\')) return;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(".aclick").one("click", function(){
jQuery("#main").append(\'<input type="text" name="tag-name" />\');
});
});
function set_myfield_value(fieldValue,fieldValue1, nonce) {
// refresh the quick menu properly
inlineEditPost.revert();
console.log(fieldValue);
console.log(fieldValue1);
jQuery(\'#myfield\').val(fieldValue);
jQuery(\'#myfield1\').val(fieldValue1);
}
</script>
<?php
}
// Add to our admin_init function
add_filter(\'post_row_actions\', \'myown_expand_quick_edit_link\', 10, 2);
function myown_expand_quick_edit_link($actions, $post) {
global $current_screen;
if (($current_screen->post_type != \'schedule\'))
return $actions;
$nonce = wp_create_nonce( \'myfield_\'.$post->ID);
$myfielvalue = get_post_meta( $post->ID, \'stime\', TRUE);
$myfielvalue1 = get_post_meta( $post->ID, \'etime\', TRUE);
$actions[\'inline hide-if-no-js\'] = \'<a href="#" class="editinline" title="\';
$actions[\'inline hide-if-no-js\'] .= esc_attr( __( \'Edit this item inline\' ) ) . \'"\';
$actions[\'inline hide-if-no-js\'] .= " onclick=\\"set_myfield_value(\'{$myfielvalue}\',\'{$myfielvalue1}\')\\" >";
$actions[\'inline hide-if-no-js\'] .= __( \'Quick Edit\' );
$actions[\'inline hide-if-no-js\'] .= \'</a>\';
return $actions;
}