Advanced Custom Fields - 这个需要附加组件Users Select, 但是,由于插件正在被完全重写,并将改变附加模块的管理,因此最好等到4.0版发布。使用强大的字段完全定制WordPress编辑屏幕。它拥有专业的界面和强大的API,对于任何使用WordPress的web开发人员来说都是必不可少的。
但是我认为simple plugin 可以为此构建:
1.
在Posts屏幕中添加一个元框,以选择书的作者(而不是post作者)。
/* Define the custom box */
add_action( \'add_meta_boxes\', \'authors_meta_box_wpse_89134\' );
/* Do something with the data entered */
add_action( \'save_post\', \'save_postdata_wpse_89134\', 10, 2 );
function authors_meta_box_wpse_89134()
{
add_meta_box(
\'sectionid_wpse_89134\',
__( \'Book authors\' ),
\'authors_box_wpse_89134\',
\'post\'
);
}
function authors_box_wpse_89134()
{
global $post;
$selected_user = get_post_meta( $post->ID, \'users_dropdown\', true);
$users_list = get_users();
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'noncename_wpse_89134\' );
echo \'<div class="element">
<select name="users_dropdown" id="users_dropdown">
<option value="">- Select -</option>\';
foreach( $users_list as $user ) {
echo \'<option value="\'.$user->ID.\'" \' . selected( $selected_user, $user->ID, false ) . \'>\'.$user->data->display_name.\'</option>\';
}
echo \'</select></div>\';
}
function save_postdata_wpse_89134( $post_id, $post_object )
{
// verify if this is an auto save routine.
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
if ( !isset( $_POST[\'noncename_wpse_89134\'] ) || !wp_verify_nonce( $_POST[\'noncename_wpse_89134\'], plugin_basename( __FILE__ ) ) )
return;
// Correct post type
if ( isset( $_POST[\'post_type\'] ) && \'post\' != $_POST[\'post_type\'] )
return;
// OK, we\'re authenticated: we need to find and save the data
//sanitize user input
$u_id = ( isset( $_POST[\'users_dropdown\'] ) ) ? intval( $_POST[\'users_dropdown\'] ) : false;
if( $u_id )
update_post_meta( $post_id, \'users_dropdown\', $_POST[\'users_dropdown\'] );
else
delete_post_meta( $post_id, \'users_dropdown\' );
}
2.
在配置文件中显示作者的书并编辑用户页面add_action( \'show_user_profile\', \'user_books_wpse_89134\' );
add_action( \'edit_user_profile\', \'user_books_wpse_89134\' );
function user_books_wpse_89134( $user )
{
$args = array(
\'post_type\' => \'post\',
\'meta_key\'=>\'users_dropdown\',
\'meta_value\'=>$user->ID,
\'meta_compare\'=>\'=\'
);
$the_query = new WP_Query( $args );
echo \'<h3>\'. __(\'User books\') .\'</h3>\';
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
endwhile;
}