如何在主页中显示自定义帖子类型?这是我的自定义代码:
// Add the Meta Box - Resorts
function add_custom_meta_box() {
add_meta_box(
\'custom_meta_box\', // $id
\'Extra Information\', // $title
\'show_custom_meta_box\', // $callback
\'resorts\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_custom_meta_box\');
// Field Array
$prefix = \'resort_\';
$custom_meta_fields = array(
array(
\'label\'=> \'Star Grade\',
\'desc\' => \'Select Star Grade\',
\'id\' => $prefix.\'star\',
\'type\' => \'select\',
\'options\' => array (
\'one\' => array (
\'label\' => \'One Star\',
\'value\' => \'1\'
),
\'two\' => array (
\'label\' => \'Two Stars\',
\'value\' => \'2\'
),
\'three\' => array (
\'label\' => \'Three Stars\',
\'value\' => \'3\'
),
\'four\' => array (
\'label\' => \'Four Stars\',
\'value\' => \'4\'
),
\'five\' => array (
\'label\' => \'Five Stars\',
\'value\' => \'5\'
))
),
array(
\'label\'=> \'Location\',
\'desc\' => \'Location ex : North Male Atoll\',
\'id\' => $prefix.\'location\',
\'type\' => \'text\'
),array(
\'label\'=> \'Overview\',
\'desc\' => \'Overview\',
\'id\' => $prefix.\'shortDesc\',
\'type\' => \'textarea\'
),array(
\'label\'=> \'Distance from airport\',
\'desc\' => \'Distance in km ex : 10km\',
\'id\' => $prefix.\'distance\',
\'type\' => \'text\'
),array(
\'label\'=> \'Transfer type\',
\'desc\' => \'Transfer type ex : Speed Boat (30 minutes) \',
\'id\' => $prefix.\'transfer\',
\'type\' => \'text\'
),array(
\'label\'=> \'Total of rooms\',
\'desc\' => \'Total rooms\',
\'id\' => $prefix.\'numrooms\',
\'type\' => \'text\'
),array(
\'label\'=> \'House Reef\',
\'desc\' => \'House reef Yes/No\',
\'id\' => $prefix.\'housereef\',
\'type\' => \'text\'
),array(
\'label\'=> \'Resort type\',
\'desc\' => \'Resort type ex: Honeymoon\',
\'id\' => $prefix.\'resorttype\',
\'type\' => \'text\'
),array(
\'label\'=> \'Resort Website\',
\'desc\' => \'Resort Website URL ex: http://www.maldivesa2z.com/\',
\'id\' => $prefix.\'resortweb\',
\'type\' => \'text\'
),array(
\'label\'=> \'Spa info Title\',
\'desc\' => \'Attach Spa Price List\',
\'id\' => $prefix.\'file1_title\',
\'type\' => \'text\'
),array(
\'label\'=> \'Attach Price List\',
\'desc\' => \'File with title\',
\'id\' => $prefix.\'file1\',
\'type\' => \'file\'
),array(
\'label\'=> \'Water Sport Title\',
\'desc\' => \'Attach Water Sport Price List\',
\'id\' => $prefix.\'file2_title\',
\'type\' => \'text\'
),array(
\'label\'=> \'Attach Price List\',
\'desc\' => \'File with title\',
\'id\' => $prefix.\'file2\',
\'type\' => \'file\'
),array(
\'label\'=> \'Dive School Title\',
\'desc\' => \'Attach Dive School Price List\',
\'id\' => $prefix.\'file3_title\',
\'type\' => \'text\'
),array(
\'label\'=> \'Attach Price List\',
\'desc\' => \'File with title\',
\'id\' => $prefix.\'file3\',
\'type\' => \'file\'
),array(
\'label\'=> \'Reservation Email\',
\'desc\' => \'Reservation Email ex: [email protected]\',
\'id\' => $prefix.\'resvemail\',
\'type\' => \'text\'
),array(
\'label\'=> \'Reservation Phone\',
\'desc\' => \'Reservation Phone ex: 00969 000 0000\',
\'id\' => $prefix.\'resvphone\',
\'type\' => \'text\'
),array(
\'label\'=> \'Reservation Fax\',
\'desc\' => \'Reservation Fax ex: 00969 000 0000\',
\'id\' => $prefix.\'resvfax\',
\'type\' => \'text\'
)
);
// The Callback
function show_custom_meta_box() {
global $custom_meta_fields, $post;
// Use nonce for verification
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// Begin the field table and loop
echo \'<table class="form-table">\';
foreach ($custom_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field[\'id\'], true);
// begin a table row with
echo \'<tr>
<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
<td>\';
switch($field[\'type\']) {
// text
case \'text\':
echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// textarea
case \'textarea\':
echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea>
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// checkbox
case \'checkbox\':
echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
<label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
break;
// select
case \'select\':
echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
}
echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
case \'file\':
echo \'<input type="file" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
<br /><span class="description">Current File : <a href="\'.get_site_url().\'/downloads/documents/\'.$meta.\'" target="_blank">\'.$meta.\'</a></span>\';
echo \'<hr>\';
break;
} //end switch
echo \'</td></tr>\';
} // end foreach
//echo \'<tr><td colspan="2">\'.str_replace(\'/wp-content/themes\', \'\', get_theme_root()).\'\\\\downloads\\\\\'.\'</td></tr>\';
//dirname($_SERVER[\'SCRIPT_FILENAME\']))
echo \'</table>\'; // end table
}
// Save the Data
function save_custom_meta($post_id) {
global $custom_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if (\'page\' == $_POST[\'post_type\']) {
if (!current_user_can(\'edit_page\', $post_id))
return $post_id;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_fields as $field) {
$old = get_post_meta($post_id, $field[\'id\'], true);
$new = $_POST[$field[\'id\']];
/* // Original save codes
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}*/
if($field[\'type\']==\'file\'):
if($_FILES[$field[\'id\']][\'name\']!=\'\'):
$newName = str_replace(\' \',\'-\',strtolower($_FILES[$field[\'id\']][\'name\']));
$newName = $post_id.\'_\'.$newName;
$destPath = str_replace(\'/wp-content/themes\', \'\', get_theme_root()).\'/downloads/documents/\'.$newName;
$done = move_uploaded_file($_FILES[$field[\'id\']][\'tmp_name\'],$destPath);
if($done):
update_post_meta($post_id,$field[\'id\'],$newName);
endif;
//update_post_meta($post_id,$field[\'id\'],$destPath);
endif;
else:
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}
endif;
}//end foreach
}
add_action(\'save_post\', \'save_custom_meta\');
//featured image
add_theme_support(\'post-thumbnails\');
//Custom Resort Type
add_action( \'init\', \'register_resort\' );
function register_resort() {
$args = array(
\'labels\' => array(
\'name\' => __( \'Resorts\' ),\'singular_name\' => __( \'Resort\' ),
\'add_new\' => _x(\'Add New\', \'review\'),
\'add_new_item\' => __(\'Add New Resort\'),
\'edit_item\' => __(\'Edit Resort\'),
\'new_item\' => __(\'New Resort\'),
\'view_item\' => __(\'View Resort\'),
\'search_items\' => __(\'Search Resort\')
),
\'taxonomies\' => array(\'category\'),
\'public\' => true,
\'publicly_queryable\' =>true,
\'show_ui\' => true,
\'query_var\' => true,
//\'menu_icon\' =>get_stylesheet_directory_uri() . \'/article16.png\',
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'thumbnail\')
);
register_post_type( \'resorts\',$args);
flush_rewrite_rules();
/*register_post_type( \'travelify_resort\',
array(
\'labels\' => array(
\'name\' => __( \'Resorts\' ),
\'singular_name\' => __( \'Resort\' )
);
array(\'public\' => true,
\'has_archive\' => true
)
);*/
}
/*
add_action( \'init\', \'my_resorts\' );
add_filter( \'post_updated_messages\', \'my_resorts_messages\' );
add_action( \'admin_head\', \'my_resorts_help\' );
function my_resorts() {
$labels = array(
\'name\' => \'Resorts\',
\'singular_name\' => \'Resort\',
\'menu_name\' => \'Resorts\',
\'name_admin_bar\' => \'Resort\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Resort\',
\'new_item\' => \'New Resort\',
\'edit_item\' => \'Edit Resort\',
\'view_item\' => \'View Resort\',
\'all_items\' => \'All Resorts\',
\'search_items\' => \'Search Resorts\',
\'parent_item_colon\' => \'Parent Resorts:\',
\'not_found\' => \'No resorts found.\',
\'not_found_in_trash\' => \'No resorts found in Trash.\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'rewrite\' => array( \'slug\' => \'resort\' ),
\'has_archive\' => true,
\'menu_position\' => 20,
\'menu_icon\' => \'dashicons-carrot\',
\'taxonomies\' => array( \'post_tag\', \'category\' ),
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'custom-fields\', \'comments\' )
);
register_post_type( \'my_resort\', $args );
}
function my_resorts_messages( $messages ) {
$post = get_post();
$messages[\'resort\'] = array(
0 => \'\',
1 => \'Resort updated.\',
2 => \'Custom field updated.\',
3 => \'Custom field deleted.\',
4 => \'Resort updated.\',
5 => isset( $_GET[\'revision\'] ) ? sprintf( \'Resort restored to revision from %s\',wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
6 => \'Resort published.\',
7 => \'Resort saved.\',
8 => \'Resort submitted.\',
9 => sprintf(
\'Resort scheduled for: <strong>%1$s</strong>.\',
date_i18n( \'M j, Y @ G:i\', strtotime( $post->post_date ) )
),
10 => \'Resort draft updated.\'
);
return $messages;
}
function my_resorts_help() {
$screen = get_current_screen();
if ( \'resort\' != $screen->post_type ) {
return;
}
$basics = array(
\'id\' => \'resort_basics\',
\'title\' => \'Resort Basics\',
\'content\' => \'Content for help tab here\'
);
$formatting = array(
\'id\' => \'resort_formatting\',
\'title\' => \'Resort Formatting\',
\'content\' => \'Content for help tab here\'
);
$screen->add_help_tab( $basics );
$screen->add_help_tab( $formatting );
}*/