我创建了一个自定义帖子类型,还添加了一些元框。我能够在前端显示自定义帖子类型以及内置元素,如帖子标题和特色图像。在我的元框中保存信息时,它会在保存帖子时清除字段。这是代码:
add_action( 'add_meta_boxes', 'fellowship_church_information' );
function fellowship_church_information() {
add_meta_box(
'fellowship_church_information', // Unique ID
__( 'Church Information', 'myplugin_textdomain' ), // Title
'fellowship_church_information_content', // Callback function
'fellowship', // Admin page (or post type)
'normal', // Context
'default' // Priority
);
}
function fellowship_church_information_content( $post ) {
echo '<div class="fellowship-church-information-form">
<label for="church_name">Church Name: </label> <input type="text" id="church_name" name="church_name" placeholder="" /> <br />
<label for="church_address">Church Address: </label> <input type="text" id="church_address" name="church_address" placeholder="" /> <br />
<label for="church_city">Church City: </label> <input type="text" id="church_city" name="church_city" placeholder="" />
<label for="church_state">Church State: </label>
<select id="church_state" name="church_state" placeholder="">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
<label for="church_zip">Church Zip: </label> <input type="text" id="church_zip" name="church_zip" placeholder="" />
<input type="hidden" name="fellowship_noncename" id="fellowship_noncename" value="' . wp_create_nonce( 'fellowship-nonce' ) . '" />
</div>
';
}
// SAVING THE INFORMATION TO THE DATABASE
add_action( 'save_post', 'fellowship_church_information_save', 10, 6 );
function fellowship_church_information_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( 'fellowship' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
if (isset($_POST['fellowship_noncename'])){
if ( !wp_verify_nonce( $_POST['fellowship_noncename'], 'fellowship-nonce' ) )
return;
}else{
return;
}
$fellowship_church_information = $_POST['fellowship_church_information'];
update_post_meta( $post_id, 'fellowship_church_information', $_POST['fellowship_church_information'] );
}
// ADD SHORTCODE -- [fellowships]
function fellowships_func( $atts ){
// DISPLAYING THE INFORMATION
$queryFellowship = array(
'post_type' => 'fellowship',
);
$fellowships = new WP_Query( $queryFellowship );
if( $fellowships->have_posts() ) {
while( $fellowships->have_posts() ) {
$fellowships->the_post();
echo '<h1>' . the_title() . '</h1>';
echo '<div class="fellowships-content">';
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content();
get_metadata();
$meta = get_post_meta( get_the_ID() );
echo $meta;
echo '</div>';
}
}
else {
echo '<h2 class="no-fellowships">There are currently no upcoming Alumni & Friends Fellowships.</h2>';
}
}
add_shortcode( 'fellowships', 'fellowships_func' );
同样,我能够显示标题和特色图像,但不能显示自定义元。