我已成功地将自定义帖子类型拉入自定义元框中的下拉列表。然而,当在前端显示它时,我还想提供一个到实际帖子的链接,而不仅仅是帖子的名称。所以我猜我需要将其保存为数组?是否可以通过下拉列表进行此操作?对我应该如何处理这件事感到困惑。非常感谢您的帮助。
以下是我目前掌握的情况:
// Add Meta Box To Select Overseeing Pastor
add_action(\'admin_init\', \'ministry_select_add_meta\');
function ministry_select_add_meta(){
add_meta_box(\'ministry_select_post\', __(\'Overseeing Pastor\'), \'ministry_select_meta\', \'ministry\', \'side\');
}
function ministry_select_meta( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values[\'pastor_select\'] ) ? esc_attr( $values[\'pastor_select\'][0] ) : \'\';
wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>
<select name="pastor_select">
<?php
$args = array(
\'post_type\' => \'employee\',
\'position\' => \'pastor\'
);
$pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
$is_selected = (get_the_title() == $selected) ? \'selected="selected"\' : \'\';
echo \'<option value="\'.get_the_title().\'" \'.$is_selected.\'>\'.get_the_title().\'</option>\';
endwhile; wp_reset_postdata();
?>
</select>
<?php
}
add_action( \'save_post\', \'ministry_select_save\' );
function ministry_select_save( $post_id )
{
// Stop If Autosaving
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
// Stop If Nonce Can\'t Be Verified
if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;
// Stop If Unauthorized User
if( !current_user_can( \'edit_post\' ) ) return;
// Make Sure Data Is Set Then Save
if( isset( $_POST[\'pastor_select\'] ) )
update_post_meta( $post_id, \'pastor_select\', esc_attr( $_POST[\'pastor_select\'] ) );
}